平面化多维数组子数组



我有一个PHP多维数组,如下所示。我想将另一个数组合并到这个数组中:

array(3) {
  [0]=>
  array(4) {
    ["id"]=> "1"
    ["register"]=> "Value 1"
    ["link"]=> "link 1"
    ["approval"]=> "yes"
  }
  [1]=>
  array(4) {
    ["id"]=> "2"
    ["register"]=> "Value 2"
    ["link"]=> "Value 2"
    ["approval"]=> "no"
  }
  [2]=>
  array(4) {
    ["id"]=> "3"
    ["register"]=> "Value 3"
    ["link"]=> "link 3"
    ["approval"]=> "pending"
  }
}

我想要合并的数组如下所示:

array(4) {
["image"]=> "123.png"
["start"]=> "8 October"
["end"]=> "9 October"
["days"]=> "2 Days"
}

每一个数组的第一个键都有一个数组,这个数组需要被合并到它里面,并且是唯一的。

我希望数组看起来像这样:

array(3) {
  [0]=>
  array(8) {
    ["id"]=> "1"
    ["register"]=> "Value 1"
    ["link"]=> "link 1"
    ["approval"]=> "yes"
    ["image"]=> "image1.png"
    ["start"]=> "8 October"
    ["end"]=> "9 October"
    ["days"]=> "2 Days"
  }
  [1]=>
  array(8) {
    ["id"]=> "2"
    ["register"]=> "Value 2"
    ["link"]=> "Value 2"
    ["approval"]=> "no"
    ["image"]=> "image2.png"
    ["start"]=> "8 October"
    ["end"]=> "9 October"
    ["days"]=> "2 Days"
  }
  [2]=>
  array(8) {
    ["id"]=> "3"
    ["register"]=> "Value 3"
    ["link"]=> "link 3"
    ["approval"]=> "pending"
    ["image"]=> "image3.png"
    ["start"]=> "8 October"
    ["end"]=> "9 October"
    ["days"]=> "2 Days"
  }
}

我尝试扁平化数组,但这会导致新的问题,因为我需要通过每个数组循环,更多的值可以在以后添加,这将使它相当大,所以我想保持每个部分在自己的数组,所以我可以根据需要拆分它以后。

try this

$arr = array(0=>array(1=>"dsfdsf",2=>"udyauyd"),1=>array(1=>"dsfdsf",2=>"udyauyd"),2=>array(1=>"dsfdsf",2=>"udyauyd"));
$arr1 = array(3=>"asdasd",4=>"fdsjldksfj",5=>"yerteruywet");
foreach($arr as $value)
{
  $new_array[] = $value + $arr1;
}
print_r($new_array);

我使用了一些示例数组来演示逻辑。

相关内容

  • 没有找到相关文章

最新更新