在PHP中获取相同索引的值

I have 2D array and want to get all values which are at same index say at index '1'. what is the best way to get that as a new array.

Example: we have array(array(1,2,3), array(5,6,7)), the result must be array(2, 6).

Thanks

A simple function would do the trick:

function foobar($array, $index) {
    $result = array();
    foreach($array as $subarray) {
        if(isset($subarray[$index])) {
            $result[] = $subarray[$index];
        }
    } 
    return $result;
}

Or you can just use array_map (requires PHP 5.3):

array_map(function($array) { return $array[1]; }, $input);
$input = array(
  array(1,2,3),
  array(5,6,7)
);

$output = array();
foreach ( $input as $data ) {
  $output[] = $data[1];
}
$myarray=array(array(1,2,3), array(5,6,7));
$index=1;

$result=array();
foreach($myarray as $a) $result[]=$a[$index];

print_r($result);
$sample = array(array(1,2,3),
                array(4,5,6),
                array(7,8,9)
               );
$index = 1;
$result = array_map(function($value) use($index) { return $value[$index]; }, $sample);
var_dump($result);