结合两个多维数组php

时间:2017-11-09 11:39:16

标签: php arrays

我有这两个数组

   Array
   (
      [0] => Array
    (
        [text] => 1ofdays.wav
    )

      [1] => Array
    (
        [text] => abcd.mp3
    )

   )



    Array
   (
      [0] => Array
    (
        [value] => 6
    )

       [1] => Array
    (
        [value] => 7
    )

)

无论如何,我可以将这两个数组合并到新的数组数组中,结果应该是

  NewArray
  (
      [0] => Array
    (
        [text] => 1ofdays.wav,
        [value]=>6
    )

     [1] => Array
    (
        [text] => abcd.mp3,,
        [value]=>7
    )

  )

当我将json编码新数组时,输出应为

  [{"text":"1ofdays.wav","value":"6"},{"text":"abcd.mp3","value","7"}]

2 个答案:

答案 0 :(得分:0)

您可以使用array_merge

轻松完成此操作
  

https://www.w3schools.com/php/func_array_merge.asp

答案 1 :(得分:0)

您可以使用array_map并以array_merge_recursive作为回调来执行此操作:

$arr1 = [['text' => '1ofdays.wav'], ['text' => 'abcd.mp3']];
$arr2 = [['value' => 6], ['value' => 7]];

$result = array_map('array_merge_recursive', $arr1, $arr2);
var_dump($result);

输出:

array (size=2)
  0 => 
    array (size=2)
      'text' => string '1ofdays.wav' (length=11)
      'value' => int 6
  1 => 
    array (size=2)
      'text' => string 'abcd.mp3' (length=8)
      'value' => int 7