Array merge on specific key Hi i heve 2 array and need to merge in one on specific key
1st array
^ array:46 [▼
"id" => 1
"q_id" => 1234
"first_name" => "111"
"last_name" => "3333"
"ad_name_surname" => "2222"
"email" => "[email protected] "
"personal_email" => "[email protected] "
"employment_status_id" => 1
"work_status_id" => 1
"citizenship_country_id" => 4
"citizenship2_country_id" => 1
"birth_date" => Illuminate\Support\Carbon @1652832000 {#1877 ▶}
"birth_city" => "Somewhere"
"country_id" => 1
"temporary_residence_address" => "Somewhere Far Away 1"
"temporary_residence_city" => "Somewhere"
"temporary_residence_city_postal_code" => "1000"
"temporary_residence_country_id" => 1
"temporary_residence_latitude" => null
"temporary_residence_longitude" => null
"permanent_residence_address" => "Somewhere Permanent 1"
"permanent_residence_city" => "There"
"permanent_residence_city_postal_code" => "7500"
"permanent_residence_country_id" => 1
"permanent_residence_latitude" => null
"permanent_residence_longitude" => null
"phone_number" => "+389111111111213121"
"emergency_contact_person" => "213123"
"emergency_contact_phone_number" => "+1112312312321321"
"personal_id_number" => "12313123"
"candidate_id" => 234
"gender_id" => 3
"education_level_id" => 1
"restricted_flag" => 0
"work_location_id" => 1
"recruitment_fee" => 0
"recruitment_fee_end_date" => null
"photo" => null
"holiday_celebration_id" => 1
"created_by" => 1
"updated_by" => 1
"email_verified_at" => null
"password" => "yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
"remember_token" => null
"created_at" => Illuminate\Support\Carbon @1636629672 {#1916 ▶}
"updated_at" => Illuminate\Support\Carbon @1651732423 {#1914 ▶}
]
and second array
^ array:5 [▼
"first_name" => "111222"
"last_name" => "3333222"
"ad_name_surname" => "22222222"
"email" => "[email protected] "
"updated_at" => "2022-05-05 07:00:27"
]
and to look something like this
array(
"first_name" => "111222"
"last_name" => "3333222"
"ad_name_surname" => "22222222"
"email" => "[email protected] "
"updated_at" => "2022-05-05 07:00:27"
),
array(
"first_name" => "111"
"last_name" => "3333"
"ad_name_surname" => "2222"
"email" => "[email protected] "
"updated_at" => "2022-05-03 07:00:27"
),
```
to show only key and value present in bout array
Why not create a skeleton, the way you want it to look like, and just call in the array value?
So:
array(
"first_name" => $array->first_name
),
array(
"first_name" => $secondArray->first_name
)
And just use loops whenever needed and push that there to 'new array' or whatever is that you're trying to do.
Wouldn't that work?
@aurelianspodarec the array are build dynamically the 1st array id the full one and the second one can be from one line to full light as the first array so I need some way to map bout array according to the second one and show only macing key
@KalimeroMK Yeah, can loop them if dynamic.
But do you mean you have something like this:
1
1-2
2
2-2
3
3-2
4
4-2
Where 1 is first array, and 1-2 is second array?
@kalimeromk
St like this
$ar11=[];
foreach($ar1 as $a1 ) $ar11[] = array_intersect_key($a1,$ar2[0]);
$result = =array_merge($ar11,$ar2);
@sr57 Undefined array key 0 pls explain the code
I did not see that ar2 was not multi dim
$ar11=[];
foreach($ar1 as $a1 ) $ar11[] = array_intersect_key($a1,$ar2);
$result = =array_merge($ar11,$ar2);
What don't you understand?
Line 1 & 2 : keep common keys in ar1 (hypothesis : all ar2 keys exists in ar1)
Line 3 : merge both array
Let's say your first array is called $array1 and your second array is called $array2.
Try this:
$valuesFromArray1 = array_intersect_key($array1, $array2);
$valuesFromArray2 = array_intersect_key($array2, $array1);
dd($valuesFromArray1, $valuesFromArray2);
array_intersect_key will return only the keys present in both arrays, and will keep the values from the left-most array.
It is a built-in PHP function:
https://www.php.net/manual/en/function.array-intersect-key.php
Please sign in or create an account to participate in this conversation.