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

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.