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

这是我输入 print_r($arrayOne); 时的输出 我该怎么办,我只能得到公司专栏,即 Agilent Technologies Inc 和 Alocoa, Inc.

我尝试了 echo $arrayOne['Company'] 和 $arrayOne[0]['Company'] 但它只输出了'Array'。

4

2 回答 2

3

你所拥有的是一个带有两个子数组的外部数组,每个子数组只有一个值。它的键是3列的描述,它的值是CSV值。如果您有 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";
  }
}

例如输入:

$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.
于 2012-11-24T15:20:24.767 回答
0

你的数组应该是这样的:

$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
于 2012-11-24T15:21:46.267 回答