PHP merge two arrays with matching values based on key - php

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

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

php multidimensional array iterative recursively

I want to insert extra key before of array if key exists and array is not multi dimensional for example:
Array
(
[0] => Array
(
[_key_] => Array
(
[0] => Array
(
[pub-id-type] => pmid
[value] => 25588809
)
[1] => Array
(
[pub-id-type] => pmc
[value] => 4302133
)
[2] => Array
(
[pub-id-type] => publisher-id
[value] => 1008
)
[3] => Array
(
[pub-id-type] => doi
[value] => 10.1186/s12885-015-1008-4
)
[type_s] => article-id
[id] => 58a6eeedeab2f
)
)
I want:
Array
(
[0] => Array
(
[_key_] => Array
(
[0]=>array(
[0] => Array
(
[pub-id-type] => pmid
[value] => 25588809
)
[1] => Array
(
[pub-id-type] => pmc
[value] => 4302133
)
[2] => Array
(
[pub-id-type] => publisher-id
[value] => 1008
)
[3] => Array
(
[pub-id-type] => doi
[value] => 10.1186/s12885-015-1008-4
)
[type_s] => article-id
[id] => 58a6eeedeab2f
)
)
)
recursively util n depath of _Key_ is found.
The layout is not the best, but howerver ...
try this one:
if(is_array($arr[0][_key_]))
{
$tmp = $arr[0][_key_];
unset($arr[0][_key_]);
$arr[0][_key_][] = $tmp;
}

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

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

Rebuild php array structure

I've been looking for the follow solution to create the array structure I need.
For a a list with updates for a specific user who can be following different things, I need to get first the followed items and the data compare to this item. my output for this is as follow.
array how it is right now
Array
(
[0] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
)
)
[1] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
)
)
)
I want to create the above array like tho on below
array like it has to be
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
[5] => Array
(
[id] => 1
[follower_id] => 1
)
)
Thanks in advice.
With kind regards,
Nicky
Seems to me that array_merge() should be sufficient:
$newArray = array_merge($myArray[0]['website_id'], $myArray[1]['website_id']);

Categories