vendredi 23 octobre 2020

How to combine or merge two nested arrays according to their index

Here is the formatted code:

<?php
$final_data1 = [
    '6882' => [
            'observation' => '51',
            'dom' => '4462',
            'amenity_value' => '5900',
            'amenity_id' => '6882',
            'amenity_name' => '10 ft. Ceiling',
            'unit_id' => [
                    '0' => '45349',
                    '1' => '45350',
                ]
        ],
    '6842' => [
            'observation' => '0',
            'dom' => '0',
            'amenity_value' => '0',
            'amenity_id' => '6842',
            'amenity_name' => '11 Ft. Ceiling',
            'unit_id' => [
                    '0' => '45317',
                    '1' => '45531'
                ]
        ]

];
$final_data2 =  [
    '6882' => [
            'observation' => '5',
            'dom' => '415',
            'amenity_value' => '150',
            'amenity_id' => '6882',
            'amenity_name' => '10 ft. Ceiling',
            'unit_id' => [
                    '0' => '45502',
                    '1' => '45505',
                    '2' => '45786'
                ]
        ]
];
$final_data  = [
    '6882' => [
            'observation' => '56', //51+5 = 56
            'dom' => '4877', //4462+415 = 4877
            'amenity_value' => '6050', //5900+150
            'amenity_id' => '6882',//no addition here id fixed
            'amenity_name' => '10 ft. Ceiling', //name fixed too
            'unit_id' => [
                    '0' => '45349',
                    '1' => '45350',
                    '2' => '45502',
                    '3' => '45505',
                    '4' => '45786' //here concatenated the two array $final_data1['6882']['unit_id'] and $final_data2['6882']['unit_id']
                ]
        ],
    '6842' => [
            'observation' => '0',
            'dom' => '0',
            'amenity_value' => '0',
            'amenity_id' => '6842',
            'amenity_name' => '11 Ft. Ceiling',
            'unit_id' => [
                    '0' => '45317',
                    '1' => '45531'
                ]
        ]

];
print_r($final_data);

Here, you can see I have two array $final_data1 and $final_data2 where first one has two index and second one as one index. Now, I want combine this two arrays to one $final_data. While combining we need to consider only those value with same index.

In this case, we will add $final_data1['6882'] and $final_data2['6882'] but index 6842 is present only in $final_data1 so this will be same in $final_data.

And, while producing final results we need to sum observation, dom, amenity_value, while amenity_id, amenity_name should be used as fixed value and unit_id should be merged or concatenated. To make this more clear I have commented it on $final_data which is the expected output.

In case you need phpsandbox online url



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3ogwXZn
via IFTTT

Aucun commentaire:

Enregistrer un commentaire