PHP - Merge 2 arrays of object using a key/id - php

I want to merge the 2 arrays of objects based on the 'id' field of Array1 and the 'itemVendorCode' of Array2. I also wanted to remove from the resulting arrays of object anything that didn't match.
Array1:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
)
[1] => stdClass Object
(
[id] => 89-575-2354
[qty] => 24
[price] => 230.35
)
[2] => stdClass Object
(
[id] => 89-605-1250
[qty] => 2
[price] => 230.35
)
)
Array2:
Array
(
[0] => Item Object
(
[internalId] => 14062
[itemVendorCode] => 89-605-1250
)
[1] => Item Object
(
[internalId] => 33806
[itemVendorCode] => 89-575-2354
)
[2] => Item Object
(
[internalId] => 64126
[itemVendorCode] => 26-295-1006
)
)

I was able to solve this by this code:
$indexed = array();
foreach($itemsArray as $value) {
$indexed[$value->itemVendorCode] = $value;
}
$results = array();
foreach($vendorItems as $obj) {
$value = $indexed[$obj->id];
if (isset($value)) {
foreach($value as $name => $val) {
$obj->$name = $val;
array_push($results, $obj);
}
}
}
print_r($results);
credits to the original poster. I just modified it a bit,
I was able to get the result like this:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[1] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[2] => stdClass Object
(
[id] => 14-102-1010
[qty] => 16
[price] => 3.2
[internalId] => 57033
[itemVendorCode] => 14-102-1010
)
)

I think you will have to use array_map function which provides you a callback function to execute on array(s).
In the callback function:
- declare your array1
- foreach the second array
- set an if statement to check that the current iteration with the id value matches the itemVendorCode of the array2 and return it
something like this:
// You have to specify to PHP to use a local copy of your $array2 to works with it into your callback
$cb = function ($obj1) use ($array2)
{
// you foreach this array
foreach ($array2 as $obj2) {
// if the value of id matches itemVendorCode
if ($obj1->id === $obj2->itemVendorCode) {
// you return the id
return $obj->id;
}
}
};
// this function will fill a new array with all returned data
$mergedArray = array_map($cb, $array1);
This code is a sample but doesn't provide you, your needled solution, try to update it to do what you exactly want ;)

Related

How to merge three arrays according to common key in php

I have three arrays first array include ids and employees name and second array have monthly collection with employee ids and third array have daily collection with employee id and daily collection I want to merge these array with ids and name and dcollection and monthly collection but the desired output is not coming here my first array $ids is
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Rohit
)
[1] => stdClass Object
(
[id] => 2
[name] => Emop1
)
[2] => stdClass Object
(
[id] => 3
[name] => Pankaj
)
[3] => stdClass Object
(
[id] => 4
[name] => tejpal singh
)
)
second array $q1 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[mcollecton] => 100
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[mcollecton] => 1222
)
)
third array $q2 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[dcollecton] => 300
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[dcollecton] => 150
)
)
so far what I have tried
$new_array = array();
foreach($ids as $k) {
$q1n = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1) {
if($k->id==$k1->id){
$mc = array("mc"=>$k1->mcollecton);
array_merge($q1n,$mc);
}
}
foreach($q2 as $k1){
if($k->id==$k1->id){
$dc = array("dc"=>$k1->dcollecton);
array_merge($q1n,$dc);
}
}
$a = array_merge($q1n,$mc);
$av = array_merge($q1n,$dc);
array_push($new_array,$q1n);
}
but the output is coming as
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
)
[1] => Array
(
[id] => 2
[name] => Emop1
)
[2] => Array
(
[id] => 3
[name] => Pankaj
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
)
)
I want the output be like
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
[mcollection] => 100
[dcollection] => 300
)
[1] => Array
(
[id] => 2
[name] => Emop1
[mcollection] => 1222
[dcollection] => 150
)
[2] => Array
(
[id] => 3
[name] => Pankaj
[mcollection] => 0
[dcollection] => 0
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
[mcollection] => 0
[dcollection] => 0
)
)
So I have tried many times but the desired output is not coming . please help me out how to get the desired output.
It seemed like that answer could be modified, or put in a function that you could call multiple times if needed to combine more than two arrays.
There's probably cleaner ways to handle this with array functions like array_merge or array_walk, but this is the general idea of how I might approach it. I haven't tested this, but maybe it's useful.
foreach($first as $key1 => $value){
foreach($second as $key2 => $value2){
// match the ids and check if array key exists on first array
if($value['id'] === $value2['id'] && empty($first[$key2])){
$first[$key][$key2] = $value2;
}
}
}
EDIT: Based on the answer you posted vs the question you asked, are you incrementing the collection numbers or just setting them? In other words why use +=? You should also be able to remove array_merge and array_push.
Below is geared more towards what you're trying to do. I haven't tested this either, but if you run into errors, post your code with the errors returned so that it's easier to debug:
foreach($ids as $k)
{
$thisArray = $newArray[] = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1)
{
if($k->id == $k1->id && !empty($k1->mcollecton))
{
$thisArray['mc'] = $k1->mcollecton;
}
}
foreach($q2 as $k2)
{
if($k->id == $k2->id && !empty($k2->dcollecton))
{
$thisArray['dc'] = $k2->dcollecton;
}
}
}
// This should have both new collections fields on all array items
print_r($newArray)

Efficient way to remove key-value from Array of Arrays

I have an array of 50000 arrays and i want to remove the "id" key-value pair from each of them.
I would rather not loop through 50k elements and was wondering if there was an efficient way to do it.
Array
(
[0] => Array
(
[id] => 713061
[market] => usd-btc
[price] => 3893.69
)
[1] => Array
(
[id] => 713056
[market] => usd-btc
[price] => 3893.69
)
[2] => Array
(
[id] => 713051
[market] => usd-btc
[price] => 3893.69
)
[3] => Array
(
[id] => 713046
[market] => usd-btc
[price] => 3893.69
)
[4] => Array
(
[id] => 713041
[market] => usd-btc
[price] => 3892.95
)
[5] => Array
(
[id] => 713036
[market] => usd-btc
[price] => 3892.95
)
I tried both the following but does not seem to be working:
// Remove ID
foreach($server_data as $sd)
{
unset($sd['id']);
}
unset($server_data['id']);
PRINT_R($server_data);
The $server_data is still returning the array with the $id element;
Any thoughts?
This creates a copy of the subarray, so when you change it, the main array is not affected:
foreach ($server_data as $sd)
{
unset($sd['id']);
}
You can unset from the original array:
foreach (array_keys($server_data) as $index)
{
unset($server_data[$index]['id']);
}
Or pass the subarray a reference so that the original is changed:
foreach ($server_data as &$sd)
{
unset($sd['id']);
}
Or, more tersely:
array_walk($server_data, function (&$item) { unset($item['id']); });
There's no reason I can think of to remove it (just ignore it), however you can run it through a callback that removes id and returns the rest:
$server_data = array_map(function($v) { unset($v['id']); return $v; }, $server_data);

PHP delete array index through value

I'm trying to unset two specific array positions which contain two values.
My actual code to fill the array.
function get_store_list(){
$data = #file_get_contents('http://www.zwinky.com/xml/clothingList.xml');
$data = #simplexml_load_string($data);
$data = $data->xpath('//stores/store');
foreach($data as $store) {
$storeArray[] = array(
"name" => (string)$store['name'],
"id" => (string)$store['id']
);
}
return $storeArray;
}
$store = get_store_list();
The array looks like the following incase ill echo it out using print_r function:
Array
(
[0] => Array
(
[name] => Artizans
[id] => 20037336
)
[1] => Array
(
[name] => Bwabies!
[id] => 20080134
)
[2] => Array
(
[name] => Crave Mart
[id] => 20097365
)
[3] => Array
(
[name] => David & Goliath
[id] => 20099998
)
[4] => Array
(
[name] => Domo
[id] => 20098166
)
[5] => Array
(
[name] => Emily the Strange
[id] => 20101926
)
[6] => Array
(
[name] => Garfield
[id] => 20098167
)
[7] => Array
(
[name] => Jetsetter
[id] => 26
)
[8] => Array
(
[name] => Like Dat
[id] => 3
)
[9] => Array
(
[name] => Paris Hilton
[id] => 21
)
[10] => Array
(
[name] => Peppermint Place
[id] => 12
)
[11] => Array
(
[name] => Rocawear
[id] => 19
)
[12] => Array
(
[name] => ShoeBuy
[id] => 10
)
[13] => Array
(
[name] => Skelanimals
[id] => 20100198
)
[14] => Array
(
[name] => Snoop Dogg
[id] => 20
)
[15] => Array
(
[name] => SW&TH
[id] => 20096121
)
[16] => Array
(
[name] => The Castle
[id] => 1
)
[17] => Array
(
[name] => The Lair
[id] => 4
)
[18] => Array
(
[name] => The Mix
[id] => 923
)
[19] => Array
(
[name] => The Powerpuff Girls
[id] => 20098121
)
[20] => Array
(
[name] => The Surf Shop
[id] => 5
)
[21] => Array
(
[name] => Tie The Knot
[id] => 20076231
)
[22] => Array
(
[name] => tokidoki
[id] => 20099224
)
[23] => Array
(
[name] => University Club
[id] => 2
)
[24] => Array
(
[name] => Z Avenue
[id] => 6
)
[25] => Array
(
[name] => Z's Greetings
[id] => 20099506
)
)
Now $store does contain 2 array indexes which I have to delete. Which are the following ids: 21 and 20076231
I've been trying the following already:
Array search code without beeing success. Does anyone have a idea what I could try?
There are a handful different approaches for this simple issue. One of them could be using function array_filter():
/**
* #param array $list the list to process
* #param array $IDsToRemove the IDs of elements to remove from $list
* #return array a subset of $list that does not contain elements having 'id' in $IDsToRemove
*/
function removeFromArray(array $list, array $IDsToRemove)
{
return array_filter(
// Filter the input list...
$list,
// ... using a function...
function (array $item) use ($IDsToRemove) {
// ... that accepts an element if its "id" is not in $IDsToRemove
return ! in_array($item['id'], $IDsToRemove);
}
);
}
// Usage
$filteredStore = removeFromArray($store, array(21, 20076231));
Try this in your loop, this will not include in your array than no need to unset like this:
foreach($data as $store) {
if($store['id'] == '20076231')
continue;
$storeArray[] = array(
"name" => (string)$store['name'],
"id" => (string)$store['id']
);
}
If you need to unset an item from your array after it's been created, take a look at array_map.
First map your array to retrieve the index of each ID.
$map = array_map(function($item){ return $item['id']; }, $store);
Then get the index of your ID from the map (e.g. 21).
$index = array_search(21, $map);
Then remove with array_splice.
array_splice($store, $index, 1);
why not use directly the id in your $storeArray? And as it seems to be integer why do you force it to (string)?
Try this:
function get_store_list(){
$data = #file_get_contents('http://www.zwinky.com/xml/clothingList.xml');
$data = #simplexml_load_string($data);
$data = $data->xpath('//stores/store');
foreach($data as $store) {
$storeArray[(int)$store['id']] = array(
"name" => (string)$store['name']
);
}
return $storeArray;
}
// delete the keys you want
unset ($storeArray[21], $storeArray[20076231]);
// or if you have more ids to delete you can create a deleteArray
$deleteArray = array(2, 20076231);
foreach ($deleteArray as $toDelete){
unset($storeArray($toDelete);
}
One line code is cool but sometimes explicit code is preferable.
function filter_by_id(array $data, $id)
{
foreach ($data as $k => &$v) {
foreach ((array) $id as $i) {
if ($v['id'] === $i) {
$v = null;
}
}
}
// 'array_filter()' produces a new array without the null entries.
// 'array_values()' produces a new array with indexes without gaps.
return array_values(array_filter($data));
}
You can filter by one id at time
$store = filter_by_id($store, 21);
Or you can filter multiple ids at the same time:
$store = filter_by_id($store, [21, 20076231]);

Intersection of two arrays using common key value for comparison

I want to perform an intersection of two arrays that have different structures, but both have one key common (fid). I want a new (filtered second) array after intersection with first array. below is my code and two arrays :
first array:
Array
(
[0] => Array
(
[fid] => 1
)
[1] => Array
(
[fid] => 3
)
)
Second array:
Array
(
[0] => Array
(
[fid] => 9
[functionality] => testing
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[2] => Array
(
[fid] => 2
[functionality] => view functionality category
[funcat_id] => 1
[name] => functionality
)
[3] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
[4] => Array
(
[fid] => 4
[functionality] => edit functionality
[funcat_id] => 1
[name] => functionality
)
)
I want this Output :
Array
(
[0] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
)
I tried this code but I'm not getting the right answer:
$result=array_intersect($array1,$array2);
//Or this also
$result=recursive_array_intersect_key($array1,$array2);
Please let me know, if any one can do this ?
I do not know if a function does exists to do this outright, but alternatively, you can just loop them instead:
$result = array();
foreach($array2 as $val2) {
foreach ($array1 as $val1) {
if($val2['fid'] == $val1['fid']) {
$result[] = $val2;
}
}
}
echo '<pre>';
print_r($result);
Sample Output
Or if you're using PHP 5.5 or greater:
$val1 = array_column($array1, 'fid');
$result = array_filter($array2, function($val2) use($val1) {
return in_array($val2['fid'], $val1);
});
foreach($array2 as $val)
{
$i=0;
foreach($array1 as $val1)
{
if($val['fid']==$val1['fid'])
{
$i++;
}
}
if($i!=0)
{
$a[]=$val;
}
}
print_r($a);

Count occurrences of a value in a column of an array of object arrays

Does anyone know how to count the occurrences of "photo" in this array:
Array (
[0] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-03-02T07:58:23+0000 )
[1] => stdClass Object ( [type] => photo [id] => 14047818930362 [created_time] => 2012-03-01T14:58:53+0000 )
[2] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-03-01T09:49:40+0000 )
[3] => stdClass Object ( [type] => status [id] => 14047818930362 [created_time] => 2012-03-01T09:36:04+0000 )
[4] => stdClass Object ( [type] => photo [id] => 14047818930362 [created_time] => 2012-02-28T07:03:25+0000 )
[5] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-02-27T09:15:34+0000 )
[6] => stdClass Object ( [type] => photo [id] => 14047818930362 [created_time] => 2012-02-27T07:32:13+0000 )
[7] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-25T09:36:57+0000 )
[8] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-02-23T08:46:43+0000 )
[9] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-22T21:04:30+0000 )
[10] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-21T20:38:27+0000 )
[11] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-02-21T07:22:44+0000 )
[12] => stdClass Object ( [type] => status [id] => 14047818930362 [created_time] => 2012-02-20T08:32:46+0000 )
[13] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-17T15:00:11+0000 ) )
To count matching occurance of a string in multidimensional array you will need to iterate over each array element and match the string and increment the count. Similiarly #Dor has suggested
$count = 0;
foreach ($array as $item) {
if ($item->type === 'photo') {
$count++;
}
}
If you want achieve same in single dimensional array then It's pretty straightforward. You can use array_count_values PHP array function as explained below.
<?php
$array = array(1, "test", 1, "php", "test");
print_r(array_count_values($array));
?>
The above example will output:
Array
(
[1] => 2
[test] => 2
[php] => 1
)
$count = 0;
foreach ($array as $item) {
if ($item->type === 'photo') {
$count++;
}
}
I'd like to acknowledge that the method by Dor Shemer is (IMO) the most direct, clean, readable, and reliable method. I just want to offer a few alternatives for those who prefer to use functional programming ...array_reduce() is a close second for me. Finally, I want to pinpoint a small gotcha for methods that use array_count_values() -- please read on...
Battery of Methods: (Demo)
$photo_count=0; // establish default value
foreach($array as $objects){
if($objects->type==='photo') ++$photo_count; // pre-increment
}
echo "foreach result = $photo_count";
echo "array_reduce = ",array_reduce($array,function($carry,$objects){return $carry+($objects->type==='photo'?1:0);},0);
echo "array_filter & count = ",sizeof(array_filter($array,function($objects){return $objects->type==='photo';}));
echo "array_column & array_filter & count = ",sizeof(array_filter(array_column($array,'type'),function($v){return $v==='photo';}));
echo "array_map & array_count_values & array_replace = ",array_replace(['photo'=>0],array_count_values(array_map(function($o) {return $o->type;}, $array)))['photo'];
echo "array_map & array_count_values (gives Notice) = ",array_count_values(array_map(function($o) {return $o->type;}, $array))['photo'];
Input/Output using OP's sample data (no trouble):
$array=[
(object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-03-02T07:58:23+0000'],
(object)['type'=>'photo','id'=>14047818930362,'created_time'=>'2012-03-01T14:58:53+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-03-01T09:49:40+0000'],
(object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-03-01T09:36:04+0000'],
(object)['type'=>'photo','id'=>14047818930362,'created_time'=>'2012-02-28T07:03:25+0000'],
(object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-02-27T09:15:34+0000'],
(object)['type'=>'photo','id'=>14047818930362,'created_time'=>'2012-02-27T07:32:13+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-25T09:36:57+0000'],
(object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-02-23T08:46:43+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-22T21:04:30+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-21T20:38:27+0000'],
(object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-02-21T07:22:44+0000'],
(object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-02-20T08:32:46+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-17T15:00:11+0000']
];
// output:
foreach result = 7
array_reduce = 7
array_filter & count = 7
array_column & array_filter & count = 7
array_map & array_count_values & array_replace = 7
array_map & array_count_values = 7
Input/Output using data with no photo values (trouble with 2nd array_count_values() method):
$array=[
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-03-01T09:49:40+0000'],
(object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-03-01T09:36:04+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-25T09:36:57+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-22T21:04:30+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-21T20:38:27+0000'],
(object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-02-20T08:32:46+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-17T15:00:11+0000']
];
// or if there are no object rows like: $array=[];
// output:
foreach result = 0
array_reduce = 0
array_filter & count = 0
array_column & array_filter & count = 0
array_map & array_count_values & array_replace = 0
array_map & array_count_values (gives Notice) = <br />
<b>Notice</b>: Undefined index: photo in <b>[...][...]</b> on line <b>43</b><br />
array_count_values() doesn't bother to generate elements with a 0 count.
Try with:
$input = array( /* your data */ );
$count = 0;
foreach ( $input as $value ) {
if ( $value->type == 'photo' ) {
$count++;
}
}

Categories