How to do sorting in PHP JSON multidimensional array by value of a certain key (ascending) and further alphabetically (other key)?

Good day!

Tell me, please, I can't find (guess) the solution.. How to do the sorting (in PHP 5.6/7) of a multidimensional JSON array by value of a certain key (ascending) and further alphabetically (other key)?

Let me explain. There's such an array:

[{
 "id": "26",
 "strana": "Thailand",
 "otel_nazvanie": "Villa Danica",
 "otel_kol_vo_zvezd": "3",
 "otel_stoimost": "22900"
}, {
 "id": "26",
 "strana": "Greece",
 "otel_nazvanie": "Vila Tufi",
 "otel_kol_vo_zvezd": "3",
 "otel_stoimost": "27700"
}, {
 "id": "26",
 "strana": "Cyprus",
 "otel_nazvanie": "Villa Kralj",
 "otel_kol_vo_zvezd": "3",
 "otel_stoimost": "28800"
}, {
 "id": "26",
 "strana": "Cyprus",
 "otel_nazvanie": "Rooms Rio",
 "otel_kol_vo_zvezd": "3",
 "otel_stoimost": "28750"
}, {
 "id": "26",
 "strana": "Thailand",
 "otel_nazvanie": "Zelena Obala",
 "otel_kol_vo_zvezd": "3",
 "otel_stoimost": "32050"
}, {
 ... // and so forth, up to 100 pieces.
}]


As it can be sorted (once, I mean at one time), to:

+ Sorted by alphabetical order of the key values strana;
+ Sorted in ascending order of key values otel_stoimost;
+ Next, output only the first element of a sorted (according to the parameters above) of the array;

That is, the result should look like this:

[{
 "id": "26",
 "strana": "Greece",
 "otel_nazvanie": "Vila Tufi",
 "otel_kol_vo_zvezd": "3",
 "otel_stoimost": "27700"
}, {
 "id": "26",
 "strana": "Cyprus",
 "otel_nazvanie": "Rooms Rio",
 "otel_kol_vo_zvezd": "3",
 "otel_stoimost": "28750"
}, {
 "id": "26",
 "strana": "Thailand",
 "otel_nazvanie": "Villa Danica",
 "otel_kol_vo_zvezd": "3",
 "otel_stoimost": "22900"
}, {
 ... // and so on.
}]


What is needed: to make the filter in the directions of tourism. The final version should go like this (example from data above):

  • Greece from 27 700 RUB.
  • Cyprus from 28 750 rubles.
  • Thailand from 22 900 RUB.


Very hope for your help, he is actually sitting with a square head.. :(
July 9th 19 at 11:08
3 answers
July 9th 19 at 11:10
Solution
What sort? Just go once through the array and select the desired data?
$a=json_decode($json, true);
$res=array();
foreach($a as $k=>$v){
if(!isset($res[$v['strana']])){
$res[$v['strana']]=$v['otel_stoimost'];
}else{
 $res[$v['strana']]=min($res[$v['strana']], $v['otel_stoimost']);
}
}
print_r($res);


Then if necessary sort the array $res alphabetically

If you need the result in a JSON structure as in your example, then add in the resulting array is not only cost but also other data.

$a=json_decode($json, true);
$res=array();
foreach($a as $k=>$v){
if(!isset($res[$v['strana']])){
$res[$v['strana']]=$v;
}else{
if($v['otel_stoimost']<$res[$v['strana']]['otel_stoimost']){
$res[$v['strana']]=$v;
}
}
}
$min_prices=array();
foreach($res as $v){
$min_prices[]=$v;
}
echo json_encode($min_prices);


If you just want the list, then enough data is received first by the way
echo '<ul>';
foreach($res as $k=>$v){
 echo '<li>'.$k.' from '.$v.' RUB</li>';
}
echo '</ul>';
Thank you. Kind of like running. Only now do not understand how to derive the array (in the form, as I wrote in the first post)? - beau80 commented on July 9th 19 at 11:13
The answer above correct answer - Emile_Swaniaws commented on July 9th 19 at 11:16
: Well, thank you, now I've understood ;) - beau80 commented on July 9th 19 at 11:19
: do not tell me how to sort this example otel_stoimost from smaller to larger number? - cullen.Douglas commented on July 9th 19 at 11:22
July 9th 19 at 11:12
July 9th 19 at 11:14
++ Sort nothing.

One pass

for(){
 if(strana.otel_stoimost < min_strana.otel_stoimost)
{
 min_strana.otel_stoimost = strana.otel_stoimost
}
}

Find more questions by tags ArraysJSONPHP