我有以下数组:
array (size=2)
0 =>
array (size=4)
0 => string 'http://localhost/wp/wp-content/uploads/2013/03/slider-area.jpg' (length=62)
1 => int 1584
2 => int 346
3 => boolean false
1 =>
array (size=4)
0 => string 'http://localhost/wp/wp-content/uploads/2013/03/featured.jpg' (length=59)
1 => int 1584
2 => int 346
3 => boolean false
我的问题是我如何循环遍历此数组以生成一个只包含两个值的新数组:
$result_array = array(0 => "http://localhost/wp/wp-content/uploads/2013/03/slider-area.jpg",
1 => "http://localhost/wp/wp-content/uploads/2013/03/featured.jpg");
我尝试了一个foreach循环,但无法获得所需的结果数组。我尝试了以下循环:
foreach ( $array as $key => $value ){
foreach ( $value as $item){
$result_array[] = $item;
}
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
应该这么简单:
$finalArray = array();
foreach($array as $arrayitem){
$finalArray[] = $arrayItem[0];
}
答案 1 :(得分:1)
你很亲密:
$result_array = array(); //Initialization is important.
foreach ($array as $value) {
$result_array[] = $value[0]; // $value[0] is the first element in the inner array.
}
答案 2 :(得分:1)
使用foreach执行此操作:
foreach( $array as $key => $value ) {
$result_array[$key] = $value[0];
}
(除非你想要遍历2个维度,否则不要在foreach中使用foreach!)
答案 3 :(得分:0)