How to join array values in PHP?

后端 未结 4 1916
囚心锁ツ
囚心锁ツ 2020-12-07 01:36

I have array 1:

Array
(
    [0] => Array
        (
            [recomendation_id] => 3588
            [employee_id] => 90141063
            


        
相关标签:
4条回答
  • 2020-12-07 02:17

    You can use array_merge_recursive function

    $result = array_merge_recursive($array1, $array2);
    
    0 讨论(0)
  • 2020-12-07 02:20

    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
            )
    
    )
    
    0 讨论(0)
  • 2020-12-07 02:30

    Yes, it is possible. Let your first array is $arr1 and second array is $arr2.

    Just assign a new field to your first array, Your first array will be updated with the second array's key, value pair.

    $arr1[0]['valuator1'] = $arr2[0]['valuator1'];
    
    print_r($arr1);
    

    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
            )
    
    )
    
    0 讨论(0)
  • 2020-12-07 02:34

    This isn't the best option in my opinion but it will still tackle your issue.

    foreach ($arr2 as $k => $v) {
        $arr1[$key] = array_merge($arr1[$key], $v);
    }
    

    This will loop through the second array and merge each value with the first array.

    Note: Your array keys should be the same in both arrays for this to work.

    0 讨论(0)
提交回复
热议问题