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');
Related
when i use
$data_collect->groupBy('group_id')
and give me result like this:
Illuminate\Support\Collection Object (
[items:protected] => Array
(
[1] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 1
[title] => Item A
[group_id] => 1
[price] => 4000
)
[1] => stdClass Object
(
[id] => 22
[title] => Gru gru
[group_id] => 1
[price] => 3000
)
)
)
[7] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 2
[title] => Item B
[group_id] => 7
[price] => 0
)
)
)
[4] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 11
[title] => Item X
[group_id] => 4
[price] => 3000
)
)
)
[6] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 12
[title] => My Book
[group_id] => 6
[price] => 3000
)
)
)
[2] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 13
[title] => Item 1A
[group_id] => 2
[price] => 3000
)
[1] => stdClass Object
(
[id] => 20
[title] => Item 1B
[group_id] => 2
[price] => 3000
)
)
)
[3] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 14
[title] => Bla bla
[group_id] => 3
[price] => 0
)
)
)
) )
But I want to filter out duplicate data to keep the lower "price" values and remove the higher "price" values. So result will be like this:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[1] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[1] => stdClass Object
(
[id] => 22
[title] => Gru gru
[group_id] => 1
[price] => 3000
)
)
)
[7] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 2
[title] => Item B
[group_id] => 7
[price] => 0
)
)
)
[4] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 11
[title] => Item X
[group_id] => 4
[price] => 3000
)
)
)
[6] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 12
[title] => My Book
[group_id] => 6
[price] => 3000
)
)
)
[2] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 13
[title] => Item 1A
[group_id] => 2
[price] => 3000
)
)
[3] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 14
[title] => Bla bla
[group_id] => 3
[price] => 0
)
)
)
)
)
Do you know how to do it? Thank you for your help.
I guess that you don't need groups in resulted collection if you need only first elemnt from each group.
$data = $data
->groupBy('group_id')
->map(function ($group) {
$sorted = $group->sortBy('price');
return $sorted->first();
//or return collect([$sorted->first()]) if you need groups in resulted collection.
});
This Question is continuation of My first question
So 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 sort this array
$sorted = $collection->sortByDesc('status');
my view
return view('voyager::users.viewusersAppraisals')->with('values', $sorted);
Now, I got array like
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[2] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[1] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[0] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
)
)
and my foreach loop
#foreach($values as $data)<?php
?>
<tr>
<td>{{$data->name}}</td>
</tr>
#endforeach
I expect output like so
linto
shanu
shelin
joseph
But I get output like so
joseph
linto
shanu
shelin
Any help would be appreciated. Thanks in advance.
It must be your variable be getting overwritten somewhere in the code which you have not mentioned.
Also please dd($sorted) your result after executing the eloquent query to see whether you are getting data from db in right format as per your need.
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.
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I need to sort this kind of information by the "score" value in a PHP script, how can I do? :
Array
(
[0] => stdClass Object
(
[name] => Morts par Déshydratation
[score] => 4
[id] => dwater
)
[1] => stdClass Object
(
[name] => Réparations de chantiers
[score] => 87
[id] => brep
)
[2] => stdClass Object
(
[name] => Campeur téméraire
[score] => 77
[id] => camp
)
[3] => stdClass Object
(
[name] => Décoration
[score] => 112
[id] => deco
)
)
PS : This is already in a PHP value, I already used json_decode.*
Where $data looks like this:
Array
(
[0] => stdClass Object
(
[name] => Morts par Déshydratation
[score] => 4
[id] => dwater
)
[1] => stdClass Object
(
[name] => Réparations de chantiers
[score] => 87
[id] => brep
)
[.] => ....
)
You can use usort() to sort the array :
<?php
usort($data, function($a, $b) { //Sort the array using a user defined function
return $a->score > $b->score ? -1 : 1; //Compare the scores
});
print_r($data);
?>
Outputs:
Array
(
[0] => stdClass Object
(
[name] => Décoration
[score] => 112
[id] => deco
)
[1] => stdClass Object
(
[name] => Réparations de chantiers
[score] => 87
[id] => brep
)
[2] => stdClass Object
(
[name] => Campeur téméraire
[score] => 77
[id] => camp
)
[3] => stdClass Object
(
[name] => Drogues
[score] => 49
[id] => drug
)
[4] => stdClass Object
(
[name] => Ouverture de porte
[score] => 11
[id] => door
)
[5] => stdClass Object
(
[name] => Morts par Déshydratation
[score] => 4
[id] => dwater
)
)
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
:)