使用foreach循环创建新变量 [英] Create new variables with foreach loop
本文介绍了使用foreach循环创建新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我明白了
$data = json_decode($json, true)["value1"];
在此之后,我将响应排序到名称值:
after this I sort the response to name value:
foreach($data as $test) {
echo $test['name'];
}
这向我显示了我需要的所有名称,效果很好.
this shows me all names I need, this worked perfectly.
Output: name1 name2 name3
问题: 我该怎么做才能在新的单个变量中获取foreach输出,以便可以在脚本的其余部分中使用它们?
Question: What have I to do, to get the foreach output in new single variables, so I can use them through the rest of the script ?
Expl.: $name_1 = name1;
也许我完全错了.
非常感谢新手编写的第一个项目.
so much thanks from a newbie who writes his 1st project.
推荐答案
这应该是您想要的:
$i = 1;
foreach($data as $test) {
${'name_'.$i++} = $test['name'];
}
尽管为此使用数组几乎总是更好的选择.
Although using an array for this is almost always the better choice.
这篇关于使用foreach循环创建新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文