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 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题