php, compare arrays and append difference - php

I have these two arrays:
1:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Type 1
[rate] => 100.00
)
[1] => stdClass Object
(
[id] => 2
[name] => Type 2
[rate] => 75.00
)
[2] => stdClass Object
(
[id] => 3
[name] => Type 3
[rate] => 50.00
)
[3] => stdClass Object
(
[id] => 4
[name] => Type 4
[rate] => 50.00
)
)
2:
Array
(
[0] => stdClass Object
(
[name] => Type 1
[rate] => 125
)
[1] => stdClass Object
(
[name] => Type 2
[rate] => 85
)
[2] => stdClass Object
(
[name] => Type 3
[rate] => 65
)
)
What I need to do is compare the two arrays, and append missing items from 1st array to the 2nd one. This will always be the case first array will have more items than the second one.
I have tried using something like:
$result = array_udiff($array1,$array2,
function ($obj_a, $obj_b) {
return $obj_a->name - $obj_b->name;
}
);
but it just returns an empty array

This?
<?php
$arr1 = array(
(object)array("id"=>1,"name"=>"type 1","rate"=>100.00),
(object)array("id"=>2,"name"=>"type 2","rate"=>75.00),
(object)array("id"=>3,"name"=>"type 3","rate"=>50.00),
(object)array("id"=>4,"name"=>"type 4","rate"=>50.00)
);
$arr2 = array(
(object)array("name"=>"type 1","rate"=>125),
(object)array("name"=>"type 2","rate"=>85),
(object)array("name"=>"type 3","rate"=>65)
);
for($i=0;$i<sizeof($arr1);$i++){
$count=0;
for($j=0;$j<sizeof($arr2);$j++){
if($arr1[$i]->name == $arr2[$j]->name){
$count++;
}
}
if($count==0){
array_push($arr2,(object)array("name"=>$arr1[$i]->name,"rate"=>$arr1[$i]->rate));
}
}
print_r($arr2);
?>

Doesn't need to be complicated, assuming that you allow the arrays to contain objects of the same type and structure. We don't have enough context given the question to understand whether there is a good reason you can't.
//$array1 original array
//$array2 target array
$array2 = array_merge($array1, $array2);

Related

get array name from multilevel array in php

how i can get WP_Widget_Archives from array,
This is my array:
$control = Array
(
[name] => Archives
[id] => archives-6
[callback] => Array
(
[0] => WP_Widget_Archives Object
(
[id_base] => archives
[name] => Archives
[widget_options] => Array
(
[classname] => widget_archive
[description] => A monthly archive of your site’s Posts.
)
[control_options] => Array
(
[id_base] => archives
)
[number] => 8
[id] => archives-8
[updated] =>
[option_name] => widget_archives
)
[1] => form_callback
)
[params] => Array
(
[0] => Array
(
[number] => 6
)
)
[width] => 250
[height] => 200
[id_base] => archives
)
i have try with this code
`echo '<pre>'; print_r(array_keys($control['callback'])); echo '</pre>';`
but I get result like this
Array
(
[0] => 0
[1] => 1
)
where I think the result will be like this
$result = Array
(
[0] => WP_Widget_Archives Object
[1] => form_callback
)
so i can write $result[0] for get WP_Widget_Archives, please help me and thank you for your help :)
Probably you misunderstood array_key function. It will give you keys of the array not value. In your case you require value which is an object 'WP_Widget_Archives', so you can directly use $control['callback'][0].

PHP json_encode() specific key of an array

I am using PHP 5.5.12.
I have an array like:
Array
(
[0] => Array
(
[user_id] => 3
[medicine_id] => 1
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_breakfast
[time] => 07:00:00
)
[1] => stdClass Object
(
[event_type] => after_breakfast
[time] => 07:30:00
)
)
)
[1] => Array
(
[user_id] => 3
[medicine_id] => 2
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_lunch
[time] => 13:00:00
)
[1] => stdClass Object
(
[event_type] => after_lunch
[time] => 14:00:00
)
)
)
[2] => Array
(
[user_id] => 3
[medicine_id] => 3
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_dinner
[time] => 20:00:00
)
[1] => stdClass Object
(
[event_type] => after_lunch
[time] => 21:00:00
)
)
)
)
I want to json_encode() the field time of each root level.
I tried using:
foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}
and:
foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}
But using print_r($user_medicine_value), it returns the same array.
I want the result to be as follows:
Array
(
[0] => Array
(
[user_id] => 3
[medicine_id] => 1
[time] => "[{"event_type":"before_breakfast","time":"07:00:00"},{"event_type":"after_breakfast","time":"07:30:00"}]"
)
[1] => Array
(
[user_id] => 3
[medicine_id] => 2
[time] => "[{"event_type":"before_lunch","time":"13:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"
)
[2] => Array
(
[user_id] => 3
[medicine_id] => 3
[time] => "[{"event_type":"before_dinner","time":"20:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"
)
)
How can I achieve this result?
I have read your question earlier and prepared the answer but you removed it before i paste the answer. Anyways here is the solution
function outer(&$val, $key) {
$val['time'] = json_encode($val['time']);
}
array_walk($your_array, 'outer');
print_r($your_array);
You can replace your foreach loop's content with something like this:
foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
$user_medicine_times[$user_medicine_key]['time'] = json_encode($user_medicine_value['time'], true);
}
Maybe the json encode fails because your time array contains an stdClass Object. Try to convert this like that :
$result = array();
foreach ($user_medicine_value['time'] as $value) {
$result['event_type'] = $value->event_type;
$result['time'] = $value->time;
}
$user_medicine_value['time'] = $result;
Because, in every iteration, the value is not being saved anywhere,
You have two options here, either make new array having time key with json_encode() or pass the value by reference as shown below.
foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
^
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}

How can I merge or search an object?

Here's my issue:
I have an object filled with arrays that look like this.
[376339] => Array
(
[0] => 1f422730-f54b-4e4d-9289-10258ce74446
[1] => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
I need to search another object to find objects with the matching IDs, like below. Is there a way of doing this without a foreach? There are SEVERAL and I would like to not have to loop over the entire object every time.
stdClass Object
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
stdClass Object
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
I need it to end up looking like this.
[376339] => Array
(
[0] => Array
(
[id] => 1f422730-f54b-4e4d-9289-10258ce74446
[percentage] => 32
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 59826
[destination_id] => 59826
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxx
)
)
)
[1] => Array
(
[id] => 60dc4646-06ce-44d0-abe9-ee371847f4df
[percentage] => 68
[destinations] => Array
(
[0] => stdClass Object
(
[id] => 60046
[destination_id] => 60046
[type] => Destination
[dequeue] =>
[value] => xxxxxxxxxxxx
)
)
)
)
I'm not sure if this makes any sense, that's why I had my two inital outputs I need to have merged into one somehow. This is all coming from one huge json object and I'm just using json_decode($jsonStuff) to decode it.
Would this be easier if I added true in the decode function? If I could just search for it like I could in python, that would be neat. But as it is, I'm at a loss as to how to get the output I need.
Note: Input json CANNOT be changed, I have no affiliation with the people that created it.
First loop over your input array and create an array with the key as the id
$input = json_decode($json_input);
$output = array();
foreach($input as $obj){
$output[$obj->id] = $obj;
}
then you can build your other array by searching the id on the array key
$massive_search_array = array(376339 => array
(
0 => 1f422730-f54b-4e4d-9289-10258ce74446,
1 => 60dc4646-06ce-44d0-abe9-ee371847f4df
)
);
$final_output = array();
foreach($massive_search_array as $index => $searches){
foreach($searches as $search){
if(isset($output[$search])){
$final_output[$index][] = $output[$search];
}
}
}

Create a grouped array from another list of array items

I have an array of apps with ids and categories like this:
[apps] => Array
(
[0] => stdClass Object
(
[id] => 0
[categoryid] => 0
)
[1] => stdClass Object
(
[id] => 31265
[categoryid] => 12
)
[2] => stdClass Object
(
[id] => 15965
[categoryid] => 2
)
[3] => stdClass Object
(
[id] => 16554
[categoryid] => 12
)
)
I am trying to get all apps for a category based on this request. So, the resultant output for:
For CategoryId 12:
----------------
[apps] => Array
(
[0] => 31265
[1] => 16554
)
For CategoryId 2:
----------------
[apps] => Array
(
[0] => 15965
)
For CategoryId 0:
----------------
[apps] => Array
(
[0] => 0
)
I believe i need to use nested foreach loops, but is there an efficient method?
Thanks
You could cycle through them, and place them into category-arrays:
foreach ( $apps as $app ) {
$catArray[ $app[CategoryID] ][] = $app;
}
This should result in an array whose key represents a category, and whose nested arrays represent those apps in that category.
I've worked up a demo of this online at: http://codepad.org/WZXIvQ58

Remove duplicates from multi-dimensional array based on higher points

I'm racking my brains trying to think of a solution. I can find plenty of solutions to remove dupes from a 2d array but I need to remove dupes where a value is lower than the other. Here is the array:
Array
(
[basketball] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 30
)
[1] => stdClass Object
(
[id] => 314
[username] => slights
[points] => 20
)
[2] => stdClass Object
(
[id] => 123
[username] => gibb54
[points] => 5
)
)
[soccer] => Array
(
[0] => stdClass Object
(
[id] => 2
[username] => Beans
[points] => 95
)
[1] => stdClass Object
(
[id] => 49
[username] => sans
[points] => 65
)
[2] => stdClass Object
(
[id] => 122
[username] => peano
[points] => 50
)
[3] => stdClass Object
(
[id] => 174
[username] => fordb
[points] => 30
)
[4] => stdClass Object
(
[id] => 112
[username] => danc
[points] => 30
)
)
)
As you may see, user ID 2, Beans is the first selection for both basketball and soccer. As they have more points for soccer, I need to remove their entry for basketball to make ID 314, slights the 0 value.
I would need to do this continually until no user be the 0 value for any of the primary array values twice.
I've tried various combinations of foreach solutions but I'm not getting anywhere. I thought a while loop would be more suitable but I don't know what condition to test for.
Any ideas please?!
I would loop through your data and create a dictionary where the keys are the user ids, and the values are the appropriate user objects with the sport appended. Then you can reconstruct your example data array structure by looping through this de-duped array using the sport data to determine where to put each user.
To create the de-duped array, use something like:
$deDupedData = array();
foreach ($data as $sport => $users) {
foreach ($users as $user) {
if (isset($deDupedData[$user->id])) {
if ($user->points > $deDupedData[$user->id]->points) {
$deDupedData[$user->id]->sport = $sport;
$deDupedData[$user->id]->points = $user->points;
}
} else {
$modifiedUser = $user;
$modifiedUser->sport = $sport;
$deDupedData[$user->id] = $modifiedUser;
}
}
}
// Now reconstruct your array...

Categories