如何操作数组

发布于 2024-12-14 20:33:27 字数 644 浏览 1 评论 0原文

所以,我有这个数组:

Array ( [idservice] => 3 [level0] => 0.35 ) Array ( [idservice] => 3 [level0] =>
0.35 ) Array ( [idservice] => 2 [level0] => 6.00 ) Array ( [idservice] => 2 [level0]
=> 6.00 ) Array ( [idservice] => 100 [level0] => 20.00 ) Array ( [idservice] => 100 
[level0] => 20.00 )

我需要将 3 作为变量添加一个前缀,使其成为 $id_3 并将 level0 设置为变量的值,就像这样

$id_3 = 0.35

,我必须对所有数组执行此操作,所以最后我会问:

$id_3   = 0.35
$id_2   = 2.00
$id_100 = 6.00

这可能吗? oi 需要 foreach 类型的语句,这样我就可以一次获取所有变量。

So, I have this array:

Array ( [idservice] => 3 [level0] => 0.35 ) Array ( [idservice] => 3 [level0] =>
0.35 ) Array ( [idservice] => 2 [level0] => 6.00 ) Array ( [idservice] => 2 [level0]
=> 6.00 ) Array ( [idservice] => 100 [level0] => 20.00 ) Array ( [idservice] => 100 
[level0] => 20.00 )

i need to make the 3 as a variable add a prefix to it to make it $id_3 and set the level0 as the value of the variable, like this

$id_3 = 0.35

and I have to do this for all the arrays, so that in the end I would have :

$id_3   = 0.35
$id_2   = 2.00
$id_100 = 6.00

is that at all possible?
o and i need foreach kind of statement so that, I can get all the variables at one time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

極樂鬼 2024-12-21 20:33:27

这应该做你想要的:

foreach ($array as $data) {
   $idVar = 'id_'.$data['idservice'];
   $idVar = $data['level0'];
}

将导致:

$id_3   = 0.35
$id_2   = 2.00
$id_100 = 6.00

This should do what you want:

foreach ($array as $data) {
   $idVar = 'id_'.$data['idservice'];
   $idVar = $data['level0'];
}

will result in:

$id_3   = 0.35
$id_2   = 2.00
$id_100 = 6.00
等你爱我 2024-12-21 20:33:27

您可以使用 extract 函数。
以下内容。

$newarray = array();
foreach ($yourarray as $array)
{
    $newkey = 'id_'.$array['idservice'];
    $newarray[$newkey] = $array['level0'];
}
extract($newarray);

您可以使用 EXTR_PREFIX_ALL 参数在每个变量名称之前添加 id 前缀。
请参阅文档此处

you could use the extract function.
Something on the following lines.

$newarray = array();
foreach ($yourarray as $array)
{
    $newkey = 'id_'.$array['idservice'];
    $newarray[$newkey] = $array['level0'];
}
extract($newarray);

You could use the EXTR_PREFIX_ALL parameter to prefix id before every variable name.
See documentation here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文