dsm42026 2012-11-24 15:17
浏览 223
已采纳

php从数组中获取特定列的所有值

Array ( [0] => Array ( [No.,"Ticker","Company"] => 
                        1,"A","Agilent Technologies Inc." ) 
        [1] => Array ( [No.,"Ticker","Company" ] => 
                        2,"AA","Alcoa, Inc.") 
      )

This is the output when i type print_r($arrayOne); What can I do so I can only get the Company column i.e. Agilent Technologies Inc, and Alocoa, Inc.

I tried echo $arrayOne['Company'] and $arrayOne[0]['Company'] but it just outputted 'Array'.

  • 写回答

2条回答 默认 最新

  • dragon88112 2012-11-24 15:20
    关注

    What you have is an outer array with two sub-arrays, each of which has only one value. Its key is the description of the 3 columns, and its value is the CSV value. If you have PHP 5.3, use str_getcsv() to parse it and return the third column

    // Loop over the outer array
    foreach ($arrayOne as  $subarr) {
      // Loop over the sub-arrays (though your sample only has one value each)
      // could also use array_pop()
      foreach ($subarr as $key => $csvstring) {
        // $csvstring holds the CSV value like  '1,"A","Agilent Technologies Inc."'
        // Parse it with str_getcsv()
        $cols = str_getcsv($csvstring);
        // Company name is the 3rd value, key [2]
        echo $cols[2] . "
    ";
      }
    }
    

    For example with the input:

    $arrayOne = array(
      array('No.,"Ticker","Company"' => '1,"A","Agilent Technologies Inc."' ), 
      array('No.,"Ticker","Company"'  => '2,"AA","Alcoa, Inc."') 
    );
    // The above loop prints:
    Agilent Technologies Inc.
    Alcoa, Inc.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了