2013-03-06 103 views
0

的PHP的foreach我有一個數組,它有它內部的兩個數組...我可以做這個訪問我想要的第一行...多維數組

print_r($_SESSION['shopcart']['cart']['qty']); 

將如何我用foreach寫這篇文章?

感謝, Ĵ

+0

什麼整個陣列是什麼? – MichaelRushton 2013-03-06 19:27:51

+0

我真的無法發佈整個陣列,因爲它有微妙的信息。 – user1269625 2013-03-06 19:28:55

+1

虛假數據沒有問題;我們只需要知道結構。 – MichaelRushton 2013-03-06 19:29:24

回答

0
foreach($_SESSION['shopcart']['cart']['qty'] as $value) { 
    echo $value; 
} 
0

,你會做這樣的事情:

轉儲陣列:$_SESSION['shopcart']['cart']

foreach($_SESSION['shopcart']['cart'] as $key=>$value){ 
    echo $key." => ".$value."<br/>"; 
} 
0

如果您想通過多個維度進行迭代,你可以嵌套如下:

foreach($_SESSION['shopcart'] as $cart) { 
    foreach ($cart as $qty) { 
     // do something 
    } 
} 

雖然我需要更多關於數組結構的信息,以及爲了提供可用的代碼而想要做的事情,但這可能是正確的。

0

我會建議你說,你這樣做:

foreach($_SESSION['shopcart'] as $key=>$value){ 
    if(is_array($value)){ 
     foreach($value => k1 => $v1){ 

     //do something here if array 

     echo $k1." => ".$v1."<br/>"; 
     } 
    }else{ 
     //do something here if not array 
    } 
}