Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join array values in PHP?

Tags:

arrays

php

I have array 1:

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            [attendance_type_id] => 2
            [start_dtm] => 2016-05-17 10:32:00
            [end_dtm] => 
            [request_message] => test notif
            [recomendation_status_id] => 1
            [last_update_dtm] => 2016-05-17 10:32:43
            [employee_name] => Nike Yulistia Angreni
            [attd_type_name] => Permittance
            [status_name] => Request
        )

)

And array 2:

Array
(
    [0] => Array
        (
            [valuator1] => Wulan  Lastia Permana
        )
)

I want to make array values in one array. I want result like this:

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            [attendance_type_id] => 2
            [start_dtm] => 2016-05-17 10:32:00
            [end_dtm] => 
            [request_message] => test notif
            [recomendation_status_id] => 1
            [last_update_dtm] => 2016-05-17 10:32:43
            [employee_name] => Nike Yulistia Angreni
            [attd_type_name] => Permittance
            [status_name] => Request
            [valuator1] => Wulan  Lastia Permana
        )
)

Is it possible to join like that?

like image 904
Nike Yulistia Angreni Avatar asked May 17 '16 07:05

Nike Yulistia Angreni


People also ask

What is the complete syntax of join () function in PHP?

echo join(" ",$arr);

How do you merge arrays?

To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.

What is use of join in PHP?

The join() function is built-in function in PHP and is used to join an array of elements which are separated by a string. Syntax string join( $separator, $array) Parameter: The join() function accepts two parameter out of which one is optional and one is mandatory.


1 Answers

You have to use array_replace_recursive:

<?php

$arr1 = Array
(
     Array
        (
            "recomendation_id" => 3588,
            "employee_id" => 90141063,
            "attendance_type_id" => 2,
            "start_dtm" => "2016-05-17 10:32:00",
            "end_dtm" => "",
            "request_message" => "test notif",
            "recomendation_status_id" => 1,
            "last_update_dtm" => "2016-05-17 10:32:43",
            "employee_name" => "Nike Yulistia Angreni",
            "attd_type_name" => "Permittance",
            "status_name" => "Request"
        )

);
$arr2 = Array
(
    Array
        (
            "valuator1" => "Wulan  Lastia Permana"
        )
);

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

Result:

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            [attendance_type_id] => 2
            [start_dtm] => 2016-05-17 10:32:00
            [end_dtm] => 
            [request_message] => test notif
            [recomendation_status_id] => 1
            [last_update_dtm] => 2016-05-17 10:32:43
            [employee_name] => Nike Yulistia Angreni
            [attd_type_name] => Permittance
            [status_name] => Request
            [valuator1] => Wulan  Lastia Permana
        )

)

Your Eval


If you use array_merge_recursive, your output will be:

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            [attendance_type_id] => 2
            [start_dtm] => 2016-05-17 10:32:00
            [end_dtm] => 
            [request_message] => test notif
            [recomendation_status_id] => 1
            [last_update_dtm] => 2016-05-17 10:32:43
            [employee_name] => Nike Yulistia Angreni
            [attd_type_name] => Permittance
            [status_name] => Request
        )

    [1] => Array
        (
            [valuator1] => Wulan  Lastia Permana
        )

)
like image 97
Thamilhan Avatar answered Sep 21 '22 18:09

Thamilhan