dongshi1606 2012-04-02 17:15
浏览 36
已采纳

遍历多维数组

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条回答 默认 最新

  • dongpao1918 2012-04-02 17:25
    关注

    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;
             // (...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化