简体   繁体   中英

Looping multidimensional array in PHP

I really can't get my head around on this multidimensional array, essentially I wanted to create a loop that will loop through all the array available in the second level (in this case is [0],[1] 2 times). Then for each loop it should return each array on the second level [id][0]=16, [product_name][0]=Foot Spa, [price][0]=69.00

Array 
(
[id] => Array 
( 
    [0] => 16 
    [1] => 17 
) 
[product_name] => Array 
( 
    [0] => Foot Spa
    [1] => Angelic Manicure 
) 
[price] => Array 
( 
    [0] => 69.00
    [1] => 42.00 
) 
)

I tried using multiple foreach loop but end up not able to stitch the values back since the loop were being separated individually.

foreach ($_SESSION['product_name'] as $eachProductName){
        echo $eachProductName;
}
foreach($inital_array as $key => $value)
{
 foreach($value as $vkey => $vvalue)
 {
   var_dump($key);
   var_dump($vkey);
   var_dump($vvalue);
   //or do sth else
 }
}

Thats how you can loop through the array, not sure how you want to use the values though....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM