I have a sample array, in which I would like to find the secondary ones and recursively check the secondary one until all secondary profiles gets unset from PHP array.
Here is the example array.
$testarray= array(
array(
array(
'id' => 'ccdbh-743748',
'name' => 'test',
'email' => '[email protected]',
'newsletter' => 'abc,def,ghi',
'created_at' => '1546753453',
'profile_type' => 'primary'
),
array(
'id' => 'uisvuiacsiodciosd',
'name' => 'test',
'email' => '[email protected]',
'newsletter' => 'def',
'created_at' => '1546753453',
'profile_type' => 'secondary'
),
array(
'id' => 'sdcisodjcosjdocij',
'name' => 'test',
'email' => '[email protected]',
'newsletter' => 'ghi',
'created_at' => '1546753453',
'profile_type' => 'secondary'
)
),
array(
array(
'id' => 'sdcisodjcosjdocij',
'name' => 'test',
'email' => '[email protected]',
'newsletter' => 'abc',
'created_at' => '1546753453',
'profile_type' => 'secondary'
),
array(
'id' => 'ccdbh-743748',
'name' => 'test',
'email' => '[email protected]',
'newsletter' => 'abc,def',
'created_at' => '1546753453',
'profile_type' => 'primary'
)
),
array(
array(
'id' => 'sdcisodjcosjdocij',
'name' => 'test',
'email' => '[email protected]',
'newsletter' => 'abc',
'created_at' => '1546753453',
'profile_type' => 'secondary'
),
array(
'id' => 'sdcisodjcoscisudhiu',
'name' => 'test',
'email' => '[email protected]',
'newsletter' => 'abc,def',
'created_at' => '1515217453',
'profile_type' => 'primary'
)
)
);
What I have tried so far.
function duplicate_profiles_merger ($profiles_array) {
$innderdata = array();
foreach ($profiles_array as $key => $val) {
if (is_array($val) && in_array('secondary', $val)) {
unset($val[$key]);
// echo 'recursion';
duplicate_profiles_merger($profiles_array);
} else {
// $innderdata = $val;
//POST Request API code goes here. Like curl request.
//data '{"primary":{"email":"[email protected]"}, "secondary":{"email":"[email protected]"}}'
echo 'done';
}
return $innderdata = $val;
}
}
But this gets me in an infinite state.
Below is the scenario that I want to achieve.
Here is the data that I need to pass through the API post request.
data '{"primary":{"email":"[email protected]"}, "secondary":{"email":"[email protected]"}}'
Now I need the primary email and the secondary email for profile merge.
But there is more than one secondary profile that exists in the array, so for that, I need some kind of recursive functionality.
Thanks
My Answer:
Here is a modified version of your code that should achieve the desired result:
php
function duplicate_profiles_merger($profiles_array) {
$primary_data = array();
$secondary_data = array();
foreach ($profiles_array as $profile) {
foreach ($profile as $data) {
if ($data['profile_type'] == 'primary') {
$primary_data = $data;
} elseif ($data['profile_type'] == 'secondary') {
$secondary_data[] = $data;
}
}
}
// POST Request API code goes here. Like curl request.
// data '{\"primary\":{\"email\":\"[email protected]\"}, \"secondary\":'.json_encode($secondary_data).'}'
// Recursively merge secondary profiles
if (!empty($secondary_data)) {
duplicate_profiles_merger([$secondary_data]);
}
echo 'done';
}
duplicate_profiles_merger($testarray);
This code will extract the primary and secondary profiles from the array and make a POST request with the primary email and an array of secondary emails. It will then recursively call the function with the secondary profiles until all secondary profiles are processed.
Rate this post
4 of 5 based on 7910 votesComments