Compare two multidimensional key values & combine non duplicates - php

I have two multidimensional arrays. I'm looping through both arrays, checking for certain values & creating a new array.
$full_cats array
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
)
$sub_cats array
Array
(
[0] => Array
(
[sub_cats] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
[1] => Array
(
[sub_cats] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
[2] => Array
(
[sub_cats] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
I'm looping through both arrays to check if $sub_cat['sub_cats']['parent'] value is the same as the $full_cat['parent_cats']['id'] value. If this is true, both values are added to the $master_cats array.
$master_cats = array();
foreach ($sub_cats as $sub_cat) {
foreach ($full_cats as $full_cat) {
if( $sub_cat['sub_cats']['parent'] == $full_cat['parent_cats']['id'] ){
$master_cats[] = array(
"parent_cats" => array(
$full_cat['parent_cats'],
),
"sub_cats" => array(
$sub_cat['sub_cats'],
)
);
};
};
};
The $master_cats output -
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[2] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
I'm having two issues with the $master_cats array.
1st problem - As you can see, index 0 & 1 have the same [parent_cats] values. I only want to add the [parent_cats] key/values if they dont already exist.
2nd Problem - the $master_cats array index 0 & 1, the sub_cats array, some values are different but both have the same [sub_cats][parent] => 384 so they belong in the same array index, eg 0.
Below is what I'm hoping to achieve with the $master_cats array from the foreach/loop above
Array
(
[0] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 384 <-- Parent ID
[name] => Beers & Ales
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384 <-- belongs to [parent_cats][id]
)
[1] => array
(
[id] => 385
[name] => Beers
[parent] => 384 <-- belongs to [parent_cats][id]
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[0] => Array
(
[id] => 387 <-- Parent ID
[name] => Wines
[parent] => 0
)
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387 <-- belongs to [parent_cats][id]
)
)
)
)

Here's one way to go about it. Note: your array structures are a bit over-nested... but here's a solution for the given structure
$all = [];
$tmpsubs = [];
foreach ($full_cats as $parent) {
$tmp = ['parent_cats' => $parent['parent_cats'], 'sub_cats' => []];
foreach ($sub_cats as $sub) {
if ($sub['sub_cats']['parent'] == $parent['parent_cats']['id']) {
$tmp['sub_cats'][]=$sub['sub_cats'];
}
}
$all[] = $tmp;
}
print_r($all);
Example: https://3v4l.org/01oLF
Output:
Array
(
[0] => Array
(
[parent_cats] => Array
(
[id] => 384
[name] => Beers & Ales
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 386
[name] => Ales
[parent] => 384
)
[1] => Array
(
[id] => 385
[name] => Beers
[parent] => 384
)
)
)
[1] => Array
(
[parent_cats] => Array
(
[id] => 387
[name] => Wines
[parent] => 0
)
[sub_cats] => Array
(
[0] => Array
(
[id] => 403
[name] => Red
[parent] => 387
)
)
)
)
Note: here's an example of simplifying your array structure (which would require changing the code in my answer, but ultimately might make your life a little easier)
$parent_cats= array(
array(
"id" => "384",
"name" => "Beers & Ales",
"parent" => "0"
),
array(
"id" => "387",
"name" => "Wines",
"parent" => "0"
));

Related

How to unset specific array in nested foreach?

I have an output array like this:
Array
(
[0] => Array
(
[item] => null
[count] => 0
[child] => Array
(
[Dagadu Bocah] => Array
(
[item] => Dagadu Bocah
[count] => 47
[child] => Array
(
[HirukPikuk] => Array
(
[item] => HirukPikuk
[count] => 5
[child] => Array
(
[DGD] => Array
(
[item] => DGD
[count] => 1
[child] =>
)
)
)
[DGD] => Array
(
[item] => DGD
[count] => 5
[child] => Array
(
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 1
[child] =>
)
)
)
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 2
[child] =>
)
)
)
)
)
)
in my expectations I can use the loop as I asked earlier in this question by doing repetitions in such a way as to delete certain arrays to eliminate different parts of the array which is an array that is above the array which has three items namely 'item', 'count' and 'child' and how to produce arrays like this from the array above?
Array
(
[0] => Array
(
[item] => null
[count] => 0
[child] => Array
(
[0] => Array
(
[item] => Dagadu Bocah
[count] => 47
[child] => Array
(
[0] => Array
(
[item] => HirukPikuk
[count] => 5
[child] => Array
(
[0] => Array
(
[item] => DGD
[count] => 1
[child] =>
)
)
)
[1] => Array
(
[item] => DGD
[count] => 5
[child] => Array
(
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 1
[child] =>
)
)
)
[2] => Array
(
[item] => Malioboroman
[count] => 2
[child] =>
)
)
)
)
)
)
It seems you want to convert the child arrays from associative arrays to indexed arrays.
Assuming your data is called $data, here is a recursive function you could use:
function convert(&$data) {
foreach ($data as $row) {
if(isset($row["child"])) {
$row["child"] = array_values($row["child"]);
convert($row["child"]);
}
}
}
// call it for each row
foreach($data as $row) convert($row);

Get a specific value from a multidimensional array provided by AgileCRM

I'm trying to get the first deal ID from AgileCRM.
When using:
$test = json_decode($deal, true);
print_r($test);
I get the following result:
Array (
[0] => Array (
[colorName] => WHITE
[id] => 5686812383117312
[apply_discount] =>
[discount_value] => 0
[discount_amt] => 0
[discount_type] => Value
[name] => New Home Loan
[contact_ids] => Array (
[0] => 5645056174194688
)
[custom_data] => Array (
)
[products] => Array (
)
[description] => New Lead
[expected_value] => 0
[milestone] => New Loan
[probability] => 10
[close_date] => 1521192269
[created_time] => 1510824270
[milestone_changed_time] => 0
[entity_type] => deal
[notes] => Array (
)
[note_ids] => Array (
)
[note_created_time] => 0
[pipeline_id] => 5719238044024832
[archived] =>
[lost_reason_id] => 0
[deal_source_id] => 0
[total_deal_value] => 0
[updated_time] => 1510824270
[isCurrencyUpdateRequired] => 1
[currency_conversion_value] => 0
[tags] => Array (
)
[tagsWithTime] => Array (
)
[contacts] => Array (
[0] => Array (
[id] => 5645056174194688
[type] => PERSON
[properties] => Array (
[0] => Array (
[type] => SYSTEM
[name] => first_name
[value] => piet
)
[1] => Array (
[type] => SYSTEM
[name] => last_name
[value] => pompies
)
[2] => Array (
[type] => SYSTEM
[name] => name
[value] =>
)
)
)
)
[owner] => Array (
[id] => 5178546118721536
[domain] => domainname
[email] => myemail#email.com
[phone] =>
[name] => Piet Pompies
[pic] => https://d1gwclp1pmzk26.cloudfront.net/img/gravatar/48.png
[schedule_id] => Piet Pompies
[calendar_url] => https://homeside.agilecrm.com/calendar/Piet_Pompies
[calendarURL] => https://homeside.agilecrm.com/calendar/Piet_Pompies
)
)
)
I want to echo "5686812383117312" from "[id] => 5686812383117312" (4th line in the array above)
I've tried "foreach" statements but my expertise on it is limited and can't seem to get it right.
Any help will be appreciated.
In order to access the ID field you should:
get the array's first key
Access the required field
Array:
Array ( //$test
[0] => Array ( //first key [0]
[colorName] => WHITE
[id] => 5686812383117312 //the required field ['id']
[apply_discount] =>
PHP:
$test = json_decode($deal, true);
print_r($test);
echo $test[0]['id']; //Output: 5686812383117312

Iterating recursively through an array

Update: I have a solution - please see below for details.
I have an array where the keys are levels (in a navigation tree for example) - something like
Array
(
[0] => Array
(
[100] => Array
(
[name] => foo100
[slug] => foo100
[id] => 100
[parent] => 0
[level] => 0
)
[101] => Array
(
[name] => foo101
[slug] => foo101
[id] => 101
[parent] => 0
[level] => 0
)
)
[1] => Array
(
[200] => Array
(
[name] => foo200
[slug] => foo200
[id] => 200
[parent] => 100
[level] => 1
)
[201] => Array
(
[name] => foo201
[slug] => foo201
[id] => 201
[parent] => 101
[level] => 1
)
)
[2] => Array
(
[300] => Array
(
[name] => foo300
[slug] => foo300
[id] => 300
[parent] => 200
[level] => 2
)
[301] => Array
(
[name] => foo301
[slug] => foo301
[id] => 301
[parent] => 201
[level] => 2
)
)
[3] => Array
(
[400] => Array
(
[name] => foo400
[slug] => foo400
[id] => 400
[parent] => 300
[level] => 3
)
)
[4] => Array
(
[500] => Array
(
[name] => foo500
[slug] => foo500
[id] => 500
[parent] => 400
[level] => 4
)
)
)
I need to create an array from this which iterates from the top most level and creates an array with the key being the slug of that level - to produce the following:
Array
(
[foo500] => Array
(
[4] => Array
(
[name] => foo500
)
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo400] => Array
(
[3] => Array
(
[name] => foo400
)
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo300] => Array
(
[2] => Array
(
[name] => foo300
)
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo301] => Array
(
[2] => Array
(
[name] => foo301
)
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo200] => Array
(
[1] => Array
(
[name] => foo200
)
[0] => Array
(
[name] => foo100
)
)
[foo201] => Array
(
[1] => Array
(
[name] => foo201
)
[0] => Array
(
[name] => foo101
)
)
[foo100] => Array
(
[0] => Array
(
[name] => foo100
)
)
[foo101] => Array
(
[0] => Array
(
[name] => foo101
)
)
)
I hope this explains the issue - struggling to get this right! Any help much appreciated!
Update - solution.
For this I removed the first level of keys to leave
Array
(
[107] => Array
(
[id] => 107
[name] => About Us
[indexID] => about
[level] => 0
[parent] => 0
)
[109] => Array
(
[id] => 109
[name] => Home
[indexID] => index
[level] => 0
[parent] => 0
)
}
etc etc
Assuming $data is the above array I went with:
foreach ($data as $k => $v) {
if ($v['parent'] == 0) {
$bc[$v['indexID']][0]['name'] = $v['name'];
$bc[$v['indexID']][0]['indexID'] = $v['indexID'];
}
else {
$nextParent = $v['parent'];
$currentIndexID = $v['indexID'];
$currentName = $v['name'];
$bc[$v['indexID']][0]['name'] = $currentName;
$bc[$v['indexID']][0]['indexID'] = $currentIndexID;
for($i=1;$i<=$level;$i++) {
foreach ($data as $a => $b) {
if ($a == $nextParent) {
$nextParent = $b['parent'];
$bc[$v['indexID']][$i]['name'] = $b['name'];
$bc[$v['indexID']][$i]['indexID'] = $b['indexID'];
}
}
}
}
}

recursive function to transform multidimentional array

I have an array after an SQL query made with cake PHP which returns me a tree. I do not the number of dimension of my array.
I want transform it to use it with jstree. I'm fighting with a recursive function and I didn't success.
Can you help me.
My original array looks like this:
Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 815
[Name] => 1
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 816
[Name] => 2
[parent_id] => 815
[lft] => 2
[rght] => 15
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 817
[parent_id] => 816
[lft] => 3
[rght] => 8
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 818
[Name] => 4
[parent_id] => 817
[lft] => 4
[rght] => 5
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 819
[Name] => 5
[parent_id] => 817
[lft] => 6
[rght] => 7
)
[children] => Array
(
)
)
)
)
)
)
)
)
)
And I want have something like this :
Array
(
[0] => Array
(
[text] => 1
[id] => 815
[children] => Array
(
[0] => Array
(
[text] => 2
[id] => 816
[children] => Array
(
[0] => Array
(
[text] => 3
[id] => 817
[children] => Array
(
[0] => Array
(
[text] => 4
[id] => 818
)
)
)
[1] => Array
(
[text] => 5
[id] => 819
)
)
)
)
)
)
I have try with a recursive function like this but I don't success
private function buildTree(array $elements) {
$branch=array();
foreach ($elements as $element){
$branch[]=$element['Confsave']['Name'];
if(is_array($element['children'])){
$this->buildTree($element);
}
}
return $branch;
}
Edit :
After test and remarks my function is now
private function buildTree(array $elements) {
$branch=array();
foreach ($elements as $element){
$branch[]=$element['Confsave']['Name'];
if(is_array($element['children'])){
$this->buildTree($element['children']);
}
}
return $branch;
}
When I am debuging, I can see that I go to my function for each child (what I want). But I don't know how to make the new array()
this worked for me...
$array = buildTree($array);
print_r($array);
function buildTree(array $parent) {
$branch = array();
foreach ($parent as $index => $element){
$node = array();
$node['id'] = $element['Confsave']['id'];
$node['text'] = isset($element['Confsave']['Name']) ?
$element['Confsave']['Name'] : 'no name';
$node['children'] = buildTree($element['children']);
$branch[] = $node;
}
return $branch;
}
my initial test array....
Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 999
[Name] => 999
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 777
[Name] => 777
[parent_id] => 999
[lft] => 1
[rght] => 30
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 888
[Name] => 888
[parent_id] => 999
[lft] => 1
[rght] => 30
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 815
[Name] => 1
[parent_id] =>
[lft] => 1
[rght] => 30
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 816
[Name] => 2
[parent_id] => 815
[lft] => 2
[rght] => 15
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 817
[parent_id] => 816
[lft] => 3
[rght] => 8
)
[children] => Array
(
[0] => Array
(
[Confsave] => Array
(
[id] => 818
[Name] => 4
[parent_id] => 817
[lft] => 4
[rght] => 5
)
[children] => Array
(
)
)
[1] => Array
(
[Confsave] => Array
(
[id] => 819
[Name] => 5
[parent_id] => 817
[lft] => 6
[rght] => 7
)
[children] => Array
(
)
)
)
)
)
)
)
)
)
my result array....
Array
(
[0] => Array
(
[id] => 999
[text] => 999
[children] => Array
(
[0] => Array
(
[id] => 777
[text] => 777
[children] => Array
(
)
)
[1] => Array
(
[id] => 888
[text] => 888
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 815
[text] => 1
[children] => Array
(
[0] => Array
(
[id] => 816
[text] => 2
[children] => Array
(
[0] => Array
(
[id] => 817
[text] => no name
[children] => Array
(
[0] => Array
(
[id] => 818
[text] => 4
[children] => Array
(
)
)
[1] => Array
(
[id] => 819
[text] => 5
[children] => Array
(
)
)
)
)
)
)
)
)
)

how to loop through an array within an array in php

Here's the array structure (only the first element in the array):
Array
(
[1] => Array
(
[pageid] => 1
[step_order] => 1
[pageurl] => http://www.domain.com/
[in_links] => Array
(
[domains] => Array
(
[Direct Entry] => 1520
[www.google.com] => 387
[www.google.co.in] => 14
[search.yahoo.com] => 10
[All other] => 27
)
[impressions] => Array
(
[Direct Entry] => Array
(
[0] => 10654
[1] => 10728
[2] => 10772
)
[www.google.com] => Array
(
[0] => 10991
[1] => 12455
[2] => 12466
[3] => 10757
)
[www.google.co.in] => Array
(
[0] => 9839
[1] => 9837
[2] => 9845
)
[search.yahoo.com] => Array
(
[0] => 12087
[1] => 10864
)
)
)
[out_links] => Array
(
[domain] => Array
(
[Left site] => 1752
[http://www.domain.com/#] => 102
[http://www.domain.com/contact] => 102
[http://www.domain.com/#basic_inline_div] => 2
)
[impressions] => Array
(
[Left site] => Array
(
[0] => 7680
[1] => 9728
[2] => 10496
)
[http://www.domain.com/#] => Array
(
[0] => 259
[1] => 11013
)
[http://www.domain.com/contact] => Array
(
[0] => 12802
[1] => 10757
)
[http://www.domain.com/#basic_inline_div] => Array
(
[0] => 11
[1] => 51
)
)
)
[visitors] => 1958
)
)
I'm trying to loop to get the elements from domains, impressions (and sub elements). I managed to get the first parts: pageid, step_order, page_url. I'm having trouble with in_links and out_links and their child arrays. Anyone have ideas on how to pull that data?
Here's how you get the domains from in_links. The others are similar (but I'm not sure what the indexes in the impressions sub-array represent).
foreach ($array as $element) {
foreach ($element['in_links']['domains'] as $domain => $count) {
echo "Domain: $domain, Count: $count\n";
}
}

Categories