我有一个数组
Array ( [0] => Array ( [username] => phizy [id] => 1 ) [1] => Array ( [username] => rapik [id] => 4 ) [2] => Array ( [用户名] => asas [id] => 5 ) )
我如何提取并将它们放入 php 变量中?
<li><?php $username." with id of ".$userid."</li>";
谢谢你
我有一个数组
Array ( [0] => Array ( [username] => phizy [id] => 1 ) [1] => Array ( [username] => rapik [id] => 4 ) [2] => Array ( [用户名] => asas [id] => 5 ) )
我如何提取并将它们放入 php 变量中?
<li><?php $username." with id of ".$userid."</li>";
谢谢你
如果你真的想把一个数组的所有键都变成变量,你可以使用这个extract()
函数。
试试这样:
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
}
只是为了好玩,如果你必须根据数组键分配变量,你可以试试这个:
foreach($array as $subArr)
{
foreach($subArr as $key=>$value)
{
$$key=$value;
}
echo "<li>$username with id of $id.</li>";
}