[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
)
)
Related
Below is an example of the array I have compiled so far. I am generating the array from a facebook graph api call and want to remove the Array wrapping each object so I just have one list under the data Array. Preferably I need a dynamic solution as their could be more than one [0] => Array, [1] => Array and so on.... in each API request.
stdClass Object
(
[data] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[id] => 21744379694_10154626935079695
[created_time] => 2016-10-16T06:29:28+0000
[from] => stdClass Object
(
[name] => Tony Hawk
[id] => 21744379694
)
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 50043151918_10154176205946919
[created_time] => 2016-10-15T20:04:22+0000
[from] => stdClass Object
(
[name] => GoPro
[id] => 50043151918
)
)
)
)
)
I would like the array to look like this ultimately. What is the best approach here?
stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[id] => 21744379694_10154626935079695
[created_time] => 2016-10-16T06:29:28+0000
[from] => stdClass Object
(
[name] => Tony Hawk
[id] => 21744379694
)
)
[1] => stdClass Object
(
[id] => 50043151918_10154176205946919
[created_time] => 2016-10-15T20:04:22+0000
[from] => stdClass Object
(
[name] => GoPro
[id] => 50043151918
)
)
)
)
Assuming your data is stored in a variable $data, you can do this:
foreach($data->data as &$el) {
$el = $el[0];
}
Now the wrapping arrays have been removed.
See it run on eval.in
You can use this:
if ($data && $data->data){ $new = array_values($data->data); }
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.
I have an array as follow in php. It contains of two array of std objects.
How can I sort it based on report_date element?
as you see report_date is unix time.
Array(
[current] => Array
(
[0] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1440963000
[id] => 3
)
[1] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1441049400
[id] => 2
)
)
[saved] => Array
(
[0] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1441049400
[id] => 2
)
[1] => stdClass Object
(
[city_fa_name] => مشهد ۳
[report_date] => 1441135800
[id] => 1
)
))
thanks in advance.
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
:)
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;
}