2013-02-12 60 views
0

我從數據庫中獲取複雜的數組,我在每次迭代(循環)中需要像下面的單獨記錄。在PHP中打印多維數組的內容

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 
       ) 

     ) 

) 

感謝很多:)

回答

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; 
} 
+0

大!但它打印只有一行,我想它的循環。 – Bheem 2013-02-12 10:59:55

+0

好,然後檢查我編輯的答案。我在編輯它。 – ripa 2013-02-12 11:02:00

+0

@Bheem哪部分是動態的? [0] - 這部分? – ripa 2013-02-12 11:05:57

0

假設$陣列是問題的陣列,

<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>