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 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?