php中的单维到多维数组

时间:2016-01-20 14:21:06

标签: php

$ results包含3个单维数组

Array ( [0] => http://ecx.images-amazon.com/images/I/51z0h9unmsL._SL160_.jpg) 
Array ( [0] => http://ecx.images-amazon.com/images/I/51z0h9unmsL._SL160_.jpg) 
Array ( [0] => http://ecx.images-amazon.com/images/I/51z0h9unmsL._SL160_.jpg) 

我想要一个结果:

Array ( [0] => http://ecx.images-amazon.com/images/I/51z0h9unmsL._SL160_.jpg) 
Array ( [1] => http://ecx.images-amazon.com/images/I/51z0h9unmsL._SL160_.jpg) 
Array ( [2] => http://ecx.images-amazon.com/images/I/51z0h9unmsL._SL160_.jpg)

2 个答案:

答案 0 :(得分:1)

解决方案1:

$new_array = array();
foreach($results as $key => $value){
 $new_array[] = $value[0];
}

或:

$new_array = array();
foreach($results as $key => $value){
 $new_array[] = $value;
}

但第一种解决方案更好,因为您的数组包含每个值的单个值。

答案 1 :(得分:1)

使用array_merge

$arr1=array("http://ecx.images-amazon.com/images/I/51z0h9unmsL._SL160_.jpg"); 
$arr2=array("http://ecx.images-amazon.com/images/I/51z0h9unmsL._SL160_.jpg"); 
$arr3=array("http://ecx.images-amazon.com/images/I/51z0h9unmsL._SL160_.jpg"); 
$arr=array_merge($arr1,$arr2,$arr3);