merge two multidimensional array based on same ID - php

I am facing one issue when merging two multidimensional arrays based on the same ID.
In the example below I created two arrays - Array1 and Array2. Both arrays contain objects that have an ID property. Based on the ID property, the arrays should be merged and get the result array:
Array1
Array
(
[0] => stdClass Object
(
[claimtotal] =>
[total] => 4
[ID] => 3
)
[1] => stdClass Object
(
[claimtotal] => 20
[total] => 1
[ID] => 4
)
)
Array2
Array
(
[0] => stdClass Object
(
[ID] =>2
[name] => test1
)
[1] => stdClass Object
(
[ID] => 3
[name] => test2
)
[2] => stdClass Object
(
[ID] =>4
[name] => test3
)
[3] => stdClass Object
(
[ID] => 5
[name] => test4
)
)
Result_array
Array
(
[0] => stdClass Object
(
[ID] =>2
[name] => test1
[claimtotal] =>
[total] =>
)
[1] => stdClass Object
(
[ID] => 3
[name] => test2
[claimtotal] =>
[total] => 4
)
[2] => stdClass Object
(
[ID] =>4
[name] => test3
[claimtotal] => 20
[total] => 1
)
[3] => stdClass Object
(
[ID] => 5
[name] => test4
[claimtotal] =>
[total] =>
)
)
How can I achieve this?

if these are simple objects without methods go:
foreach($firstArray as $key => $firstObject){
foreach($secondArray as $secondObject){
if($firstObject['id'] === $secondObject['id']){
$firstArray[$key] = (object) array_merge((array) $firstObject, (array) $secondObject);
}
}
}
looks messy but does the job without introducing another loop to go through object properties.

Related

usort not working for laravel multidimensional arrays

I have an array
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[1] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[2] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
)
)
I want to rearrange this array by status desc order
I try
usort($usersdetailsA, function($a, $b) {
return $a->status <=> $b->status;
});
I got an error like
usort() expects parameter 1 to be array, object given
I tried
$usersdetailsA = $this->$usersdetailsA->getValues();
the i got an error like
Undefined property:
TCG\Voyager\Http\Controllers\Users::$[{"id":79,"name":"shelin","status":0},{"id":80,"name":"shanu","status":"2"},{"id":81,"name":"linto","status":"2"},{"id":82,"name":"joseph","status":0}]
Expected output
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[1] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[2] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
Any help would be appreciated.Thanks in advance
You can sort a collection with sort methods by a given key:
$sorted = $collection->sortByDesc('status');

PHP Sorting Objects

[sections] => stdClass Object
(
[22353] => stdClass Object
(
[id] => 2
[section_start_date] => 1410235200
)
[22354] => stdClass Object
(
[id] => 1
[section_start_date] => 1410235260
)
)
How do I sort the above objects in PHP by the id, while preserving the keys of the sections object? For instance I want to show 22354 on top of 22353. Since these are objects the keys are technically just strings to me but I need to keep them in tact.
There is some confusion going on. These are objects which are not in an array. Pay close attention to the section object.
this is how you do it
stdClass Object
(
[111111] => stdClass Object
(
[id] => 2
[section_start_date] => 1410235200
)
[999999] => stdClass Object
(
[id] => 1
[section_start_date] => 1410235260
)
[222222] => stdClass Object
(
[id] => 1
[section_start_date] => 1410235300
)
[555555] => stdClass Object
(
[id] => 1
[section_start_date] => 1410231160
)
)
Steps
Convert stdClass to array
$data = json_decode(json_encode($object),true);
ksort($data);
print_r($data);
output new sorted array while maintaining key index.
Array
(
[111111] => Array
(
[id] => 2
[section_start_date] => 1410235200
)
[222222] => Array
(
[id] => 1
[section_start_date] => 1410235300
)
[555555] => Array
(
[id] => 1
[section_start_date] => 1410231160
)
[999999] => Array
(
[id] => 1
[section_start_date] => 1410235260
)
)

Tree(or hierarchy) array to two dimensional structured array in php

How can I convert a tree like array to two dimensional linear array? Still now I am stuck into this question. May be somebody ask this question but yet now I didn't get a proper way.
$str='[{"id":1},{"id":2,"children":[{"id":3,"children":[{"id":4}]},{"id":5,"children":[{"id":6},{"id":7,"children":[{"id":8}]}]},{"id":9},{"id":10}]},{"id":11,"children":[{"id":12}]}]'
This is my json string when I convert it to array using json_decode() function. I got the following result
Array
(
[0] => stdClass Object
(
[id] => 1
)
[1] => stdClass Object
(
[id] => 2
[children] => Array
(
[0] => stdClass Object
(
[id] => 3
[children] => Array
(
[0] => stdClass Object
(
[id] => 4
)
)
)
[1] => stdClass Object
(
[id] => 5
[children] => Array
(
[0] => stdClass Object
(
[id] => 6
)
)
)
[2] => stdClass Object
(
[id] => 7
)
[3] => stdClass Object
(
[id] => 8
)
)
)
[2] => stdClass Object
(
[id] => 9
[children] => Array
(
[0] => stdClass Object
(
[id] => 10
)
)
)
)
This is like a tree structure but I need to convert it into a two dimensional array like
Array
(
[0] => Array
(
[parent] => 0
[id] => 1
)
[1] => Array
(
[parent] => 0
[id] => 2
)
[2] => Array
(
[parent] => 2
[id] => 3
)
[3] => Array
(
[parent] => 3
[id] => 4
)
[4] => Array
(
[parent] => 2
[id] => 5
)
[5] => Array
(
[parent] => 5
[id] => 6
)
[6] => Array
(
[parent] => 2
[id] => 7
)
[7] => Array
(
[parent] => 2
[id] => 8
)
[8] => Array
(
[parent] => 0
[id] => 9
)
[9] => Array
(
[parent] => 0
[id] => 10
)
)
You'll need to write a recursive function and/or a loop to sort your object into an array like the one you specified.
I'll start by saying that in future if you could post sample data and expected output that actually matches what you want and expect that would be great - because I just spent ten minutes working out why my function is telling me that the parent of id 7 is id 5 when your example says it should be 2, when your JSON and your example array are actually different - annoying.
Anyway, here's the idea:
Recursive function to check for children in the passed array. If they exist, call the function again on its children. Add the current to the output array regardless of whether there are children or not.
&$output = output variable passed by reference, so you don't need to return anything and can access the output variable without a global call.
$parent_id represents the parent id each time you're iterating over children. In the first cases where the node doesn't have a parent, the declaration $parent_id = 0 will define your "default" parent ID. Otherwise, the parent ID is passed each time you go deeper.
function checkForChildrenOtherwiseAddToArray(&$output, $array, $parent_id = 0) {
// loop through all sub-arrays inside this instance (if any)
foreach($array as $each) {
// check for children
if(isset($each->children)) {
// go deeper, passing in children and parent ID
checkForChildrenOtherwiseAddToArray($output, $each->children, $each->id);
}
// add current iteration to array as well
$output[] = array(
'parent' => $parent_id,
'id' => $each->id
);
}
}
Example use:
$your_array = json_decode($str);
$output = array();
checkForChildrenOtherwiseAddToArray($output, $your_array);
This will give you results that are seemingly un-ordered because of the recursive nature of this function. To sort by id for example you could use usort():
usort($output, function($a, $b) {
return $a['id'] - $b['id'];
});
... and your example output would be:
Array
(
[0] => Array
(
[parent] => 0
[id] => 1
)
[1] => Array
(
[parent] => 0
[id] => 2
)
[2] => Array
(
[parent] => 2
[id] => 3
)
[3] => Array
(
[parent] => 3
[id] => 4
)
[4] => Array
(
[parent] => 2
[id] => 5
)
[5] => Array
(
[parent] => 5
[id] => 6
)
[6] => Array
(
[parent] => 5
[id] => 7
)
[7] => Array
(
[parent] => 7
[id] => 8
)
[8] => Array
(
[parent] => 2
[id] => 9
)
[9] => Array
(
[parent] => 2
[id] => 10
)
[10] => Array
(
[parent] => 0
[id] => 11
)
[11] => Array
(
[parent] => 11
[id] => 12
)
)

PHP Accessing And Searching a multidimensional array

I have the following array:
Array (
[0] => stdClass Object (
[#attributes] => stdClass Object (
[Id] => 0 [MapId] => 1 [Name] => [LocationId] => 0 [MapRelativeX] => 0.58813826735513464 [MapRelativeY] => 0.5223214285714286
)
)
[1] => stdClass Object ( [#attributes] => stdClass Object ( [Id] => 1 [MapId] => 1 [Name] => GL D [LocationId] => 1 [MapRelativeX] => 0.54053714859437729 [MapRelativeY] => 0.43601190476190477 ) ) [2] => stdClass Object ( [#attributes] => stdClass Object ( [Id] => 2 [MapId] => 1 [Name] => GL E [LocationId] => 1 [MapRelativeX] => 0.458028542742398 [MapRelativeY] => 0.5223214285714286 ) )
I am trying to access and search through this array. For instance I am trying to access the ID key/value pair but I can't. I have the following code.
$row->Id doesn't return anything where $row is $fullArray[0]
Also is there a way to search this array for the row with the ID=2?
Thanks
figured it out:
$row[0]->attributes()->Id
:)

Sort stdClass Object after an array

I have two arrays,
Array
(
[0] => stdClass Object
(
[id] => 1
[title] => art
)
[1] => stdClass Object
(
[id] => 4
[title] => adsdf
)
[2] => stdClass Object
(
[id] => 2
[title] => adsdf
)
[3] => stdClass Object
(
[id] => 7
[title] => adsdf
)
)
Array
(
[2] => 2
[1] => 1
)
And I want to sort the first array after the second array. In the second array the key and the value is equal with the first array id. So the output have to be the following.
Array
(
[0] => stdClass Object
(
[id] => 2
[title] => adsdf
)
[1] => stdClass Object
(
[id] => 1
[title] => art
)
[2] => stdClass Object
(
[id] => 4
[title] => adsdf
)
[3] => stdClass Object
(
[id] => 7
[title] => adsdf
)
)
You can use array_multisort[Docs] for it:
array_multisort($arraySort, $arrayData);
Pass the array with the sort order as the first and your array to be sorted as the second parameter.
You might need to build the sort array prior to it, from your question it's not clear to me if you already have it or not.
In case not, if you want to get all of the data arrays entries ID values into the sort array:
$arraySort = array();
foreach($arrayData as $key => $obj)
{
$arraySort[$key] = $obj->id;
}

Categories