eval.in

Paste #1058846

PHP — PHP 5.6.23, pasted 6 months ago (json)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
$array = array(
  array("id" => "1", "name" => "name1"),
  array("id" => "2", "name" => "name2"),
  array("id" => "3", "name" => "name3"),
  array("id" => "4", "name" => "name4"),
  array("id" => "5", "name" => "name5"),
  array("id" => "6", "name" => "name6"),
  array("id" => "7", "name" => "name7"),
  array("id" => "8", "name" => "name8"),
  array("id" => "9", "name" => "name9"),
  array("id" => "10", "name" => "name10"),
  array("id" => "11", "name" => "name11"),
  array("id" => "12", "name" => "name12"),
);

$array1 = array(
  array("id" => "1", "description" => "description1"),
  array("id" => "2", "description" => "description2"),
  array("id" => "3", "description" => "description3"),
  array("id" => "4", "description" => "description4"),
  array("id" => "5", "description" => "description5"),
  array("id" => "6", "description" => "description6"),
  array("id" => "7", "description" => "description7"),
  array("id" => "8", "description" => "description8"),
  array("id" => "9", "description" => "description9"),
  array("id" => "10", "description" => "description10"),
  array("id" => "11", "description" => "description11"),
  array("id" => "12", "description" => "description12"),
);

$array = array_column($array, null, 'id');
$array1 = array_column($array1, null, 'id');

$merged = array_replace_recursive($array, $array1);

foreach ($merged as $row) {
  echo "name is {$row['name']} and description is {$row['description']}", PHP_EOL;
}

Program Output

name is name1 and description is description1
name is name2 and description is description2
name is name3 and description is description3
name is name4 and description is description4
name is name5 and description is description5
name is name6 and description is description6
name is name7 and description is description7
name is name8 and description is description8
name is name9 and description is description9
name is name10 and description is description10
name is name11 and description is description11
name is name12 and description is description12

OK (0.008 sec real, 0.007 sec wall, 14 MB, 65 syscalls)

Fork