我正在从数据库中跟踪复杂数组,我需要在每次迭代(循环)中使用如下所示的单独记录。
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
)
)
)
非常感谢:)
答案 0 :(得分:3)
如果$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;
}
答案 1 :(得分:0)
假设$ 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>