遍历多维数组

时间:2012-04-02 17:15:21

标签: php arrays loops multidimensional-array

我正在与Magento API进行交互,并且在调用之后:

$result = $soap->call( $session_id, 'catalog_product.list' );

我得到一个数组,里面装满了包含信息的数组,我知道这是因为在执行了print_f之后我得到了以下结果:

Array( [0] => Array( [product_id] => 2 [sku] => 401HCS [name] => Paul Penders Hydrating Control Serum (20g) [set] => 4 [type] => simple [category_ids] => 

Array ( [0] => 4 [1] => 15 [2] => 43 ) )

[1] => Array ( [product_id] => 3 [sku] => 400ICT [name] => Paul Penders Intensive Clarifying Therapy (ICT) [set] => 4 [type] => simple [category_ids]

Array ( [0] => 4 [1] => 11 [2] => 43 ) ) 

[2] => Array ( [product_id] => 4 [sku] => 402CFE [name] => Paul Penders Herbal Citrus Fruit Exfoliant (60ml) [set] => 4 [type] => simple [category_ids] => 

它显然没有缩进,我这样做是为了便于阅读,所以我的问题是如何遍历某种循环以进入每个数组并获得[product_id]和其他元素?提前谢谢!

1 个答案:

答案 0 :(得分:5)

它标记为'java',但显然是php。你可以这样遍历它:

foreach ($result as $id => $data) {
   foreach ($data as $key => $value) {
      switch ($key) {
         case 'product_id':
            // do things
            break;
         case 'sku':
            // do things
            break;
         // (...)