Traversing a multi-dimensional array

不问归期 提交于 2020-01-04 05:39:12

问题


I'm interacting with the Magento API and after calling:

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

I get an array, full of arrays with information inside of them, I know this because after performing print_f on it I get the following result:

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

It is not indented obviously, i did that for easy reading, so my question is how would I go about traversing some kind of loop in order to go into every array and get the [product_id] and the other elements? Thanks in advance!


回答1:


It's tagged 'java' but it clearly is php. You can traverse it like this:

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


来源:https://stackoverflow.com/questions/9980775/traversing-a-multi-dimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!