[英]php getting all values of a specific column from an array
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);这是我输入 print_r($arrayOne) 时的输出; What can I do so I can only get the Company column ie Agilent Technologies Inc, and Alocoa, Inc.我该怎么做,我只能得到 Company 列,即 Agilent Technologies Inc 和 Alocoa, Inc。
I tried echo $arrayOne['Company'] and $arrayOne[0]['Company'] but it just outputted 'Array'.我试过 echo $arrayOne['Company'] 和 $arrayOne[0]['Company'] 但它只是输出了'Array'。
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.它的key是3列的描述,value是CSV值。 If you have PHP 5.3, use str_getcsv()
to parse it and return the third column如果你有 PHP 5.3,使用str_getcsv()
解析它并返回第三列
// 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] . "\n";
}
}
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.
Your array should be like this:你的数组应该是这样的:
$arrayOne = array(array('no' => 1, 'ticker' => 'A', 'company' => 'Agilent Technologies Inc.'),
array('no' => 2, 'ticker' => 'AA', 'company' => 'Alcoa Inc.'));
echo $array[0]['company']; // will print Agilent Technologies
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.