How to Recursively find secondary profiles and merge into primary one - php

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' => 'testemail#test.com',
'newsletter' => 'abc,def,ghi',
'created_at' => '1546753453',
'profile_type' => 'primary'
),
array(
'id' => 'uisvuiacsiodciosd',
'name' => 'test',
'email' => 'testemail#test.com',
'newsletter' => 'def',
'created_at' => '1546753453',
'profile_type' => 'secondary'
),
array(
'id' => 'sdcisodjcosjdocij',
'name' => 'test',
'email' => 'testemail#test.com',
'newsletter' => 'ghi',
'created_at' => '1546753453',
'profile_type' => 'secondary'
)
),
array(
array(
'id' => 'sdcisodjcosjdocij',
'name' => 'test',
'email' => 'testemail#test.com',
'newsletter' => 'abc',
'created_at' => '1546753453',
'profile_type' => 'secondary'
),
array(
'id' => 'ccdbh-743748',
'name' => 'test',
'email' => 'testemail#test.com',
'newsletter' => 'abc,def',
'created_at' => '1546753453',
'profile_type' => 'primary'
)
),
array(
array(
'id' => 'sdcisodjcosjdocij',
'name' => 'test',
'email' => 'testemail#test.com',
'newsletter' => 'abc',
'created_at' => '1546753453',
'profile_type' => 'secondary'
),
array(
'id' => 'sdcisodjcoscisudhiu',
'name' => 'test',
'email' => 'testemail#test.com',
'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":"cool.person#company.com"}, "secondary":{"email":"cperson#gmail.com"}}'
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":"cool.person#company.com"}, "secondary":{"email":"cperson#gmail.com"}}'
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

This answer sums up the comment section:
You call your recursion method with the same exact array you started with. But PHP is passing arguments as value as default, meaning you only modify the $var element, but not the $profiles_array, because of this you are creating an infinite loop.
Plus: There is no need for recursion here because there is no "child-structure" that would profit from real recursion here. A simple loop should do the trick.
function duplicate_profiles_merger ($profiles_array) {
$innderdata = array();
foreach ($profiles_array as $key => $val) {
if (is_array($val) && $val['profile_type'] == 'primary') {
array_push($innerdata, $val);
}
}
return $innerdata;
}
This should be all there is to it. You can also modify the existing array, eg. deleting all secondary profiles. Then you have to pass the array as reference, not value: https://www.php.net/manual/en/language.references.pass.php

Related

Replace key in array, with keeping order intact

I would like to replace keys in arrays, because I will move them on two indexes up.
Problem that I am facing is that those are containing same names which will not be ok, if i want to move them up.
This is how array looks like.
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '3; top',
),
1 => array(
'Name' => 'level',
'value' => '1; blue',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GSSD154',
),
),
),
),
1 => array(
'messagetype' => 'Information',
'visibility' => 'Private',
'info' => array(
0 => array(
'urgency' => 'Minor',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '1; left',
),
1 => array(
'Name' => 'level',
'value' => '1; red',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GBECS23',
),
),
),
),
),
),
),
),
);
and this is how I would like the output to be with changing keys in Name0, Name1, which are inside params.
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params' => array(
0 => array(
'Name0' => 'display',
'value0' => '3; top',
),
1 => array(
'Name1' => 'level',
'value1' => '1; blue',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GSSD154',
),
),
),
),
1 => array(
'messagetype' => 'Information',
'visibility' => 'Private',
'info' => array(
0 => array(
'urgency' => 'Minor',
'params' => array(
0 => array(
'Name0' => 'display',
'value0' => '1; left',
),
1 => array(
'Name1' => 'level',
'value1' => '1; red',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GBECS23',
),
),
),
),
),
),
),
),
);
I have tried with a lots of examples over this website, but could not find one to achieve this.
Code that I used from
How to replace key in multidimensional array and maintain order
function replaceKey($subject, $newKey, $oldKey) {
// if the value is not an array, then you have reached the deepest
// point of the branch, so return the value
if (!is_array($subject)) {
return $subject;
}
$newArray = array(); // empty array to hold copy of subject
foreach ($subject as $key => $value) {
// replace the key with the new key only if it is the old key
$key = ($key === $oldKey) ? $newKey : $key;
// add the value with the recursive call
$newArray[$key] = replaceKey($value, $newKey, $oldKey);
}
return $newArray;
}
$s = replaceKey($list, 'Name0', 'Name');
print "<PRE>";
print_r($s);
at the moment I get this output:
[0] => Array
(
[Name0] => display
[value] => 1; left
)
[1] => Array
(
[Name0] => level
[value] => 1; red
)
any help would be appreciated. regards
A very strange question, but why not?
The following function returns nothing (a procedure) and changes the array in-place using references but feel free to rewrite it as a "real" function (without references and with a return statement somewhere).
The idea consists to search for arrays, with numeric keys and at least 2 items, in which each item has the Name and value keys. In other words, this approach doesn't care about paths where the targets are supposed to be:
function replaceKeys(&$arr) {
foreach ($arr as &$v) {
if ( !is_array($v) )
continue;
$keys = array_keys($v);
if ( count($keys) < 2 ||
$keys !== array_flip($keys) ||
array_keys(array_merge(...$v)) !== ['Name', 'value'] ) {
replaceKeys($v);
continue;
}
foreach ($v as $k => &$item) {
$item = array_combine(["Name$k", "value$k"], $item);
}
}
}
replaceKeys($list);
print_r($list);
demo

php loop over multidimensional array can combine same key/value into new array element

I am getting some data out of mysql into an array like this
array(
[0] = array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '555555'
),
[1] = array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '666666'
),
[2] = array(
'code' => '234567',
'title' => 'something else',
'price' => '3.00',
'other_value' => '333333'
),
[3] = array(
'code' => '345678',
'title' => 'another thing',
'price' => '4.00',
'other_value' => NULL
),
)
what i need to do is for each row, if the key code appears more than once, merge the rows into one but create a new array for the other_value like so
array(
[0] = array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => array(
[0] => '555555',
[1] => '666666'
)
),
[1] = array(
'code' => '234567',
'title' => 'something else',
'price' => '3.00',
'other_value' => '333333'
),
[2] = array(
'code' => '345678',
'title' => 'another thing',
'price' => '4.00',
'other_value' => NULL
),
)
What is the best way to achieve this?
I did think about looping over the each row and checking for existence of thtat key/value then do something if it exists.
#AdRock i hope you want to merge array in case of when 'code' will be same, if this is so then try below one:
<?php
$arr = array(
array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '555555'
),
array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '666666'
),
array(
'code' => '234567',
'title' => 'something else',
'price' => '3.00',
'other_value' => '333333'
),
array(
'code' => '345678',
'title' => 'another thing',
'price' => '4.00',
'other_value' => NULL
)
);
echo "<pre>";
print_r($arr);// array before
$isExist = array();
foreach($arr as $key => $value){
if(in_array($value["code"], $isExist)){
$getKey = array_search($value["code"], $isExist);
$arr[$getKey]["other_value"] = array($arr[$getKey]["other_value"], $value["other_value"]);
unset($arr[$key]);
}
else{
$arr[$key] = $value;
}
$isExist[$key] = $value["code"];
}
echo "<pre>";
print_r(array_values($arr));// array after
?>
The approach that I would suggest is to loop through the array and store the value of code into a new array and store the entire result set into a new array. With each iteration, check whether the value is present or not in the code value stored array. And if value found then in that case, get the key of the code array and use the same key for the result array and store it inside the other_value. Hope it makes sense.
Looping over the array and creating a new array using the 'code' values as keys might be the simplest method. In that case you can check if the key allready exists.
$new_array=array();
foreach($array as $part){
$code_as_key = $part['code'];
//if code allready in the new array, just add a new value
if( isset($new_array[$code_as_key]) ){
$new_array[$code_as_key]['other_value'][] = $part['other_value'];
}
//else add the new code
else{
$new_array[$code_as_key]=$part;
}
}
//re-index the new array, starting from key 0
$new_array=array_values($new_array);

de-dupe array of documents

I run a solr query, which returns a result set of jobs.
Some of the jobs are duplicates (but from different sources), which is decided based on if the job title, description and location are the same.
I want to loop through my result set, and combine any duplicated into one job, with that one job having multiple sources... something like:
original result:
$jobs = array(
array(
'id' => 'job1',
'title' => 'test',
'description' => 'test',
'location' => 'test',
'source' => 'source1',
),
array(
'id' => 'job2',
'title' => 'test',
'description' => 'test',
'location' => 'test',
'source' => 'source2',
),
array(
'id' => 'job3',
'title' => 'test',
'description' => 'test',
'location' => 'test',
'source' => 'source3',
),
array(
'id' => 'job4',
'title' => 'testing',
'description' => 'testing',
'location' => 'testing',
'source' => 'source1',
)
);
would become:
$jobs = array(
array(
'id' => 'job1',
'title' => 'test',
'description' => 'test',
'location' => 'test',
'source' => 'source1',
'other_sources' => array(
array(
'id' => 'job2',
'title' => 'test',
'description' => 'test',
'location' => 'test',
'source' => 'source2'
),
array(
'id' => job3,
'title' => 'test',
'description' => 'test',
'location' => 'test',
'source' => 'source3'
),
),
),
array(
'id' => 'job4',
'title' => 'testing',
'description' => 'testing',
'location' => 'testing',
'source' => 'source1'
)
);
how can I achieve this? Either in PHP or perhaps in the Solr query itself (I'm using Solarium to do my Solr querying)
How about something like this?
<?php
$result = array();
foreach ($jobs as $job) {
if (!empty($result[$job['title']])) {
$result[$job['title']]['other_sources'][] = $job;
}
else {
$result[$job['title']] = $job;
}
}
It initializes an empty array($result) and then loops through the job array. The empty array will store the jobs with the title being used as the key. If the job title does not exist in the result array, then it will add it. If the job title does exist in the job array, then it will append the job to an array inside the existing job (under the key 'other_sources')

Unique Merge multiple multidimensional array

I have some difficulties to merge many multidimensional array in php. I tried to do it by many way, but each time, I don't get the result wanted. I tried with array_merge(array_unique,...) and in different post I found a way with array_map, but I don't understand everything...
I can have many multi array like below:
array(
(int) 0 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'AM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
),
(int) 1 => array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'PM'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
)
And I would like to get this kind of array :
array(
'User' => array(
'username' => 'testje',
'firstname' => 'jean',
'lastname' => 'test'
),
'Calendar' => array(
'period' => 'Full day'
),
'Shift' => array(
'name' => 'HV',
'color' => '#b7fa00'
),
'Team' => array(
'name' => 'Proxy_B28'
)
)
Do you have some advices to give me to get this result ?
Thank you very much !
I don't know if the best solution but it seems to work like this, and fastly :
foreach ($users as $k=>$v){
//$r[$k] = array_merge($v,$users[$k]);
//$unique[] = array_map("unserialize", array_unique(array_map("serialize", $users[$k])));
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => $v['Calendar']['period']
);
if ($k > 0) {
if (in_array($v['User']['username'],$s[$k])) {
unset($s[$k-1]);
$s[$k] = array(
'username' => $v['User']['username'],
'team' => $v['Team']['name'],
'period' => "FD"
);
}
}
}
Do you have another idea or this one is enough good ?
thank you !

Extracting data from complicated associative array in php and put into new array

I have an complicated array that looks like this:
$input=array(
(int) 0 => array(
'XXX' => array(
'id' => '7',
'p_id' => '1',
'address' => '9463',
'arrival_time' => '2014-05-01 03:30:00'
),
'YYY' => array(
'id' => '1',
'iden' => '1111',
'name' => 'Tom'
)
),
(int) 1 => array(
'XXX' => array(
'id' => '9',
'p_id' => '2',
'address' => '9469',
'arrival_time' => '2014-05-27 16:43:58'
),
'YYY' => array(
'id' => '2',
'iden' => '2222',
'name' => 'Sam'
)
),
(int) 2 => array(
'XXX' => array(
'id' => '3',
'p_id' => '3',
'address' => '9462',
'arrival_time' => '2014-04-21 14:05:00'
),
'YYY' => array(
'id' => '3',
'iden' => '3333',
'name' => 'James'
)
)
)
I would like to convert it such that it looks like this;
$output=array(
(int) 0 => array(
'name' => 'Tom',
'iden' => '1111',
'address' => '9463'
),
(int) 1 => array(
'name' => 'Sam',
'iden' => '2222',
'address' => '9469'
),
(int) 2 => array(
'name' => 'James',
'iden' => '3333',
'address' => '9462'
)
I wrote some code to solve this problem:
foreach ( $input as $key => $value)
{
$output['name']=$input[$key]['YYY']['name'];
$output['iden']=$input[$key]['YYY']['iden'];
$output['address']=$input[$key]['XXX']['address'];
}
Unfortunately, it retrieves only the last element of the input array.
Can someone more experienced help?
Thank you very much.
You are overwriting the values in each iteration, as you always write to $output['name'] etc.
foreach ( $input as $key => $value)
{
$output[$key] = array(
'name' => $value['YYY']['name'],
'iden' => $value['YYY']['iden'],
'address' => $value['XXX']['address']
);
}
The key here is using $output[$key] instead of $output - this way you will add a new element in each iteration.
Also $input[$key] and $value are equivalent, so I used the shorter variant ;)
Try this in your foreach loop :-
foreach ( $input as $key=>$value)
{
$output[$key]['name']=$value['YYY']['name'];
$output[$key]['iden']=$value['YYY']['iden'];
$output[$key]['address']=$value['XXX']['address'];
}
You have to add an index to the array in the foreach: $output[$key]["name"] = ...;

Categories