我有一个数组
数组([0] =>数组([用户名] => phizy [id] => 1)[1] =>数组([用户名] => rapik [id] => 4 )[2] =>数组([用户名] => asas [id] => 5))
我如何提取并将它们放入php变量中?
<li><?php $username." with id of ".$userid."</li>";
谢谢
答案 0 :(得分:1)
试试这样:
foreach($array as $arobj)
{
?>
<li><?php $arobj['username']." with id of ".$arobj['id']; ?></li>
<?php
}
或者,如果要分配给变量,那么
foreach($array as $arobj)
{
$username = $arobj['username'];
$userid = $arobj['id'];
?>
<li><?php $username." with id of ".$userid; ?></li>
<?php
}
答案 1 :(得分:1)
如果您真的想将数组的所有键转换为变量,可以使用extract()
函数。
答案 2 :(得分:0)
只是为了好玩,如果你必须根据数组键分配变量,你可以试试这个:
foreach($array as $subArr)
{
foreach($subArr as $key=>$value)
{
$$key=$value;
}
echo "<li>$username with id of $id.</li>";
}