如何在php中将一个数组值分配给另一个数组

时间:2014-01-10 16:20:08

标签: php arrays

例如:

$variable = Array(
[0] => Array( [id]=>"1" [file]=>"new" )
[1] => Array( [id]=>"2" [file]=>"sample" )
[2] => Array( [id]=>"3" [file]=>"hello" )
[3] => Array( [id]=>"4" [file]=>"garden" )
[4] => Array( [id]=>"5" [file]=>"door" )
[5] => Array( [id]=>"6" [file]=>"tem" )
)

如何将所有文件行分配到一个数组值

$test =$variable["file"]
print_r($test);

输出:

Array( 
[0] => "new" 
[1] => "sample 
[2] => "hello" 
[3] => "garden" 
[4] => "door" 
[5] => "tem"
)

3 个答案:

答案 0 :(得分:6)

如果您使用的是PHP 5.5,则可以使用array_column()

 $new_array = array_column($array, 'file');

答案 1 :(得分:2)

我实际上一直这样做:

$test = array();
foreach($variable as $v){
  $test[] = $v['file'];
}
print_r($test);

答案 2 :(得分:0)

$result = array_map(function($item) {return $item['file'];}, $variable);
print_r($result);