How to unset specific array in nested foreach? - php

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);

Related

Compare two multidimensional key values & combine non duplicates

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"
));

PHP merge two arrays with matching values based on key

I have two arrays; the first one has a set of instructions to delete what post from the second array.
The First Array
Array
(
[0] => Array
(
[post_delete] => 1
[post_key] => YXdsrP
)
[1] => Array
(
[post_delete] => 1
[post_key] => 1jV37Q
)
[2] => Array
(
[post_delete] => 1
[post_key] => JvlMIL
)
)
The Second Array
Array
(
[0] => Array
(
[post_id] => 598490e010c7c25738677bd0
[post_key] => TbjI1A
)
[1] => Array
(
[post_id] => 598490c210c7c24938677bd1
[post_key] => YXdsrP
)
[2] => Array
(
[post_id] => 598490a41908cd2619677bd0
[post_key] => myYIKh
)
[3] => Array
(
[post_id] => 59847e2a10c7c2155ff80e82
[post_key] => 1jV37Q
)
[4] => Array
(
[post_id] => 59847e076bf29fec56f274c2
[post_key] => JvlMIL
)
)
I am looking to combine these two arrays and create one based on matching the keys labeled "post_key" within each array, while dropping the others from the second array. The end result I am looking for.
Array
(
[0] => Array
(
[post_delete] => 1
[post_key] => YXdsrP
[post_id] => 598490c210c7c24938677bd1
)
[1] => Array
(
[post_delete] => 1
[post_key] => 1jV37Q
[post_id] => 59847e2a10c7c2155ff80e82
)
[2] => Array
(
[post_delete] => 1
[post_key] => JvlMIL
[post_id] => 59847e076bf29fec56f274c2
)
)

Compare values of two multidimentional array and insert if not exits

I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){

Get values from multiple arrays in PHP

This is my array which is coming from a foreach loop:
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Administratie,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Administratie,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Anderemailgroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Anderemailgroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Beheergroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Beheergroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Belangrijke Groep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Belangrijke Groep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Hoofdgroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Hoofdgroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Mailgroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Mailgroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Testgroep2,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Testgroep2,OU=Test,DC=stefan,DC=nl
)
)
The question is, how do I get all the CN="GroupName" values from all the arrays in a list or something? It only needs to grab that value from every single array and display it in a list.
For example:
I only want this value from every array.
And the output should be like this:
Administratie
Anderemailgroep
Beheergroep
Belangrijke Groep
Hoofdgroep
Mailgroep
Testgroep2
EDIT
The array is coming from this piece of code:
$result = $adldap->user()->groups('test.user');
for ($i=0;$i<count($result);$i++) {
sort($result);
}
print_r($result);
foreach ($result as $key => $value) {
$check = $adldap->group()->info($value, array(
'distinguishedname'
));
if (strpos($check[0]['distinguishedname'][0], 'OU=Test') !== false) {
unset($result[$key]);
print_r($check);
}
}
Lets say your array is $arrs which contains of your array values given.
You can do like this.
<?php
foreach($arrs as $arr) {
foreach($arr as $ar) {
$sep = explode(',',$ar['dn']);
echo explode('=',$sep[0])[1];
}
}
?>

Pull out a PHP variable in a complex nested array (from a Drupal View) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I've got a complex array variable called $data. I need to pull out one very buried value: [url] in [field_website]. Here's the raw print_r() output of the $data variable:
stdClass Object ( [node_title] => CMI2 [nid] => 3 [field_data_field_website_node_entity_type] => node [field_data_field_blog_node_entity_type] => node [field_data_field_rss_node_entity_type] => node [field_data_field_twitter_node_entity_type] => node [field_data_field_yammer_node_entity_type] => node [field_data_field_facebook_node_entity_type] => node [field_data_field_flickr_node_entity_type] => node [field_data_field_youtube_node_entity_type] => node [_field_data] => Array ( [nid] => Array ( [entity_type] => node [entity] => stdClass Object ( [vid] => 3 [uid] => 1 [title] => CMI2 [log] => [status] => 1 [comment] => 1 [promote] => 0 [sticky] => 0 [nid] => 3 [type] => social_source [language] => und [created] => 1356040541 [changed] => 1356040541 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1356040541 [revision_uid] => 1 [field_website] => Array ( [und] => Array ( [0] => Array ( [url] => http://cmi2.yale.edu [title] => [attributes] => Array ( ) ) ) ) [field_blog] => Array ( ) [field_rss] => Array ( ) [field_twitter] => Array ( [und] => Array ( [0] => Array ( [url] => http://twitter.com/yalecmi2 [title] => [attributes] => Array ( ) ) ) ) [field_facebook] => Array ( ) [field_youtube] => Array ( ) [field_flickr] => Array ( ) [field_yammer] => Array ( ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item [1] => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date [1] => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1356040541 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => admin [picture] => 0 [data] => b:0; ) ) ) [field_field_website] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://cmi2.yale.edu [#access] => 1 ) [raw] => Array ( [url] => http://cmi2.yale.edu [title] => http://cmi2.yale.edu [attributes] => Array ( ) [display_url] => http://cmi2.yale.edu ) ) ) [field_field_blog] => Array ( ) [field_field_rss] => Array ( ) [field_field_twitter] => Array ( [0] => Array ( [rendered] => Array ( [#markup] => http://twitter.com/yalecmi2 [#access] => 1 ) [raw] => Array ( [url] => http://twitter.com/yalecmi2 [title] => http://twitter.com/yalecmi2 [attributes] => Array ( ) [display_url] => http://twitter.com/yalecmi2 ) ) ) [field_field_yammer] => Array ( ) [field_field_facebook] => Array ( ) [field_field_flickr] => Array ( ) [field_field_youtube] => Array ( ) )
(sorry about that ugliness!)
How the heck do I pull out that [url] variable?! Ideally, I just want to assign that one value to another variable or just print it out.
If it's helpful, this is from a Drupal 7 view with the Views PHP module.
Thanks!
I recommend reading up about PHP arrays and objects since what you are trying to do is so trivial.
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/sdo.sample.getset.php

Categories