0

Hi i have problem with merge array,

How to merge array from different array

From Here

Array 1

array:6 [
  "patient_name" => "Pasien 4"
  "employee_no" => "1114"
  "birth_date" => "1990-05-02"
  "gender" => "L"
  "department_code" => "D0004"
  "section_code" => "S0004"
]

Array 2

 array:2 [
  "kd_layan" => "10000104  "
  "nama_layan" => "PAKET MCU ADVANCE (MALE)"
]

To Here

array:8 [
  "patient_name" => "Pasien 4"
  "employee_no" => "1114"
  "birth_date" => "1990-05-02"
  "gender" => "L"
  "department_code" => "D0004"
  "section_code" => "S0004"
  "kd_layan" => "10000104  "
  "nama_layan" => "PAKET MCU ADVANCE (MALE)"
]

Any solution for this problem?

Thanks

3

5 Answers 5

1

It has very simple solution using array_merge() function of php. array_merge() function merges one or more arrays into one array.

You can assign one array to the function, or as many as you like. If two or more array elements have the same key, the last one overrides the others.

in your case use it as below

$arr1=[
    "patient_name" => "Pasien 4",
    "employee_no" => "1114",
    "birth_date" => "1990-05-02",
    "gender" => "L",
    "department_code" => "D0004",
    "section_code" => "S0004"
];
$arr2=[
    "kd_layan" => "10000104  ",
    "nama_layan" => "PAKET MCU ADVANCE (MALE)"
];

print_r(array_merge($arr1,$arr2));

for more see documentation

Sign up to request clarification or add additional context in comments.

Comments

1

use array_merge to merge two array

$array1 = [
  "patient_name" => "Pasien 4",
  "employee_no" => "1114",
  "birth_date" => "1990-05-02",
  "gender" => "L",
  "department_code" => "D0004",
  "section_code" => "S0004",
];

$array2 = [
  "kd_layan" => "10000104",
  "nama_layan" => "PAKET MCU ADVANCE (MALE)"
];

$res = array_merge($array1, $array2);

echo '<pre>';
print_r($res);

check demo code

3 Comments

Hi thanks from answer but from this case here is my all structure data pastebin.com/J9BjV91x How to merge data from every data from array to above
You should have something common to merge one array to that particular another array. otherwise, how can we predict that the first array will merge with forth. you should have something that are common in the array. Or if possible make it using join query, :)
The answer you've selected is the same answer of mine and I posted first. then why you selected his answer?
1

Use array_merge()

It merges one or more arrays into one array.

Syntax : array_merge(array1, array2, array3, ...)

Comments

1

use array_merge

$arr1 =  [
  "patient_name" => "Pasien 4"
  "employee_no" => "1114"
  "birth_date" => "1990-05-02"
  "gender" => "L"
  "department_code" => "D0004"
  "section_code" => "S0004"
]

 arr2 = [
  "kd_layan" => "10000104  "
  "nama_layan" => "PAKET MCU ADVANCE (MALE)"
]

$result  = array_merge($arr1, $arr2);

echo '<pre>';
print_r($result);

Comments

1

Below i have mention an example which will merge two array and the output will be as it is as you want.

$a = array('1' => 'one','2' => 'two');

$b = array('3' => 'three','4' => 'four');

$c = ($a + $b);

print_r($c);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.