从两个数组创建一个数组并分别组合值

时间:2018-12-17 10:29:23

标签: php arrays sorting

如果我的问题被问错,请提出一个我可以使用的标题。无论如何,这是我的问题。

我有一个看起来像这样的数组:

Array
 (
  [0] => Array
    (
       [0] => pathToAnImage
       [1] => pathToAnImage
       [2] => pathToAnImage
       [3] => pathToAnImage
  [1] => Array
    (
       [0] => 4 
       [1] => 2
       [2] => 1 
       [3] => 3 
   )
)

现在,此数组来自三个数组:

array_push($mergedArrays, $basePath, $imagesPath, $sorting)

其中$basePath$imagePath返回图像的完整路径,而$sorting是排序数据库值。

问题是我如何合并这些值?我想要的结果看起来像这样:

Array
 (
  [0] => Array 
    (
    [0] => Array
         [0] => pathToAnImage
         [1] => 4 
    [1] => Array
         [0] => pathToAnImage
         [1] => 2
    [2] => Array
         [0] => pathToAnImage
         [1] => 1
    [3] => Array
         [0] => pathToAnImage
         [1] => 3 
  )
)

2 个答案:

答案 0 :(得分:2)

使用 array_columnarray_merge

$yourarray = array
 (
  array
    (
       "0" => "pathToAnImage",
       "1" => "pathToAnImage",
       "2" => "pathToAnImage",
       "3" => "pathToAnImage",
    ),
  array
    (
       "0" => "4", 
       "1" => "2",
       "2" => "1", 
       "3" => "3",
   )
);


$arr1 = $yourarray[0];
$arr2 = $yourarray[1];


foreach ($arr1 as $key => $value){
    $out[] =  array_merge((array)$arr2[$key], (array)$value);
}
echo "<pre>";
print_r($out);

DEMO

答案 1 :(得分:0)

@ splash58为我指明了正确的方向,并且我的代码解决方案发现了我的问题:

$new = array_map(null, ...$array);