我正在从数据库中获取以下复杂数组,在每次迭代(循环)中,我需要像下面这样的单独记录。
subscribed_store -> Name
subscribed_companyLogo -> logo image
subscribed_merchant -> Company Name
我们怎样才能简化以下内容。
/********************** First Iteration *******************/
Array
(
[subscribed_store] => Array
(
[0] => stdClass Object
(
[id] => 55
[name] => test
)
)
[subscribed_companyLogo] => Array
(
[0] => http://test.com/images/logo.png
)
[subscribed_merchant] => Array
(
[0] => stdClass Object
(
[id] => 9
[company_name] => Google
[company_url] => http://www.google.com
[first_name] => David
)
)
)
非常感谢:)
如果$array
持有上述数组,则尝试
echo $array["subscribed_store"][0]->name;
echo $array["subscribed_companyLogo"][0];
echo $array["subscribed_merchant"][0]->company_name;
如果你想以动态的方式,那么请参阅下面的
foreach($array as $key=>$val)
{
foreach($val as $subkey=>$subval)
{
echo $subval->id;
}
}
这段代码将全局工作,并将回显id。但您需要回显不同的元素,这些元素是不同的键。所以使用以下
foreach($array["subscribed_store"] as $key=>$val)
{
echo $val->name;
}
foreach($array["subscribed_companyLogo"] as $key=>$val)
{
echo $val;
}
foreach($array["subscribed_merchant"] as $key=>$val)
{
echo $val->company_name;
}
假设$array是问题中的数组,
<table>
<tr>
<td> Store </td>
<td> Company Logo </td>
<td> Company Name </td>
</tr>
<?php
foreach($array as $key => $row)
{
?>
<tr>
<td> <?php echo $row['subscribed_store'][0]->name;?> </td>
<td> <?php echo $row['subscribed_companyLogo'][0];?> </td>
<td> <?php echo $row['subscribed_merchant'][0]->company_name;?> </td>
</tr>
<?php
}
?>
</table>