遍历多维数组 [英] Traversing a multi-dimensional array

查看:65
本文介绍了遍历多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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!

推荐答案

它被标记为'java',但显然是php.您可以像这样遍历它:

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;
         // (...)

这篇关于遍历多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆