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
:)
Related
[zzcrewssadd] => stdClass Object
(
[items] => stdClass Object
(
[item] => stdClass Object
(
[0] => abc
[#0] => stdClass Object
(
[no] => 1
[id] => 01_00_00_00_00
[depth] => 1
[itemCnt] => 126647
)
[1] => def
[#1] => stdClass Object
(
[no] => 2
[id] => 01_01_00_00_00
[depth] => 2
[itemCnt] => 37119
)
[2] => ghi
[#2] => stdClass Object
(
[no] => 3
[id] => 01_01_01_00_00
[depth] => 3
[itemCnt] => 13730
)
)
)
)
How do I approach it?
The code I tried.
zzcrewssadd->items->item[#0];
This will result in an error.
This will result in an error.
This will result in an error.
This will result in an error.
Why don't you run a foreach loop on this zzcrewssadd->items->item
or try this to access object keys
echo zzcrewssadd->items->item->{'#0'};
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');
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.
[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
)
)