V
V
Vic Shostak2016-05-27 12:30:18
PHP
Vic Shostak, 2016-05-27 12:30:18

How to sort in PHP a multidimensional JSON array by the value of a certain key (in ascending order) and additionally alphabetically (by other key)?

Good afternoon!
Tell me, please, I can’t find (guess) a solution in any way .. How to sort (in PHP 5.6/7) a multidimensional JSON array by the value of a certain key (in ascending order) and additionally alphabetically (by other key)?
Let me explain. There is this array:

[{
  "id": "26",
  "strana": "Таиланд",
  "otel_nazvanie": "Villa Danica",
  "otel_kol_vo_zvezd": "3",
  "otel_stoimost": "22900"
}, {
  "id": "26",
  "strana": "Греция",
  "otel_nazvanie": "Vila Tufi",
  "otel_kol_vo_zvezd": "3",
  "otel_stoimost": "27700"
}, {
  "id": "26",
  "strana": "Кипр",
  "otel_nazvanie": "Villa Kralj",
  "otel_kol_vo_zvezd": "3",
  "otel_stoimost": "28800"
}, {
  "id": "26",
  "strana": "Кипр",
  "otel_nazvanie": "Rooms Rio",
  "otel_kol_vo_zvezd": "3",
  "otel_stoimost": "28750"
}, {
  "id": "26",
  "strana": "Таиланд",
  "otel_nazvanie": "Obala Zelena",
  "otel_kol_vo_zvezd": "3",
  "otel_stoimost": "32050"
}, {
  ... // и так далее, до 100 штук.
}]

How can it be sorted (at once, well, that is, immediately at one time) so that it:
+ Sorted alphabetically from the value of the key strana ;
+ Sorted in ascending order from the otel_stoimost key value ;
+ Further, only the first element of the sorted (by parameters above) array was displayed;
That is, the result should look like this:
[{
  "id": "26",
  "strana": "Греция",
  "otel_nazvanie": "Vila Tufi",
  "otel_kol_vo_zvezd": "3",
  "otel_stoimost": "27700"
}, {
  "id": "26",
  "strana": "Кипр",
  "otel_nazvanie": "Rooms Rio",
  "otel_kol_vo_zvezd": "3",
  "otel_stoimost": "28750"
}, {
  "id": "26",
  "strana": "Таиланд",
  "otel_nazvanie": "Villa Danica",
  "otel_kol_vo_zvezd": "3",
  "otel_stoimost": "22900"
}, {
  ... // и так далее.
}]

What is it for: to make a filter by tourism destinations. The final version should come out like this (for example, from the data above):
  • Greece from 27,700 rubles.
  • Cyprus from 28 750 rubles.
  • Thailand from 22 900 rubles.

I really hope for your help, I myself am already really sitting with a square head .. :(

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
imhuman, 2016-05-27
@vikkyshostak

Why sort? Just go through the array once 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 $res array alphabetically.
If you need a JSON structure as in your example, then add not only the cost, but also other data to the final array.
$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 need a list output, then the data obtained in the first way is enough
echo '<ul>';
foreach($res as $k=>$v){
    echo '<li>'.$k.' от '.$v.' руб.</li>';
}
echo '</ul>';

A
Andrey Burov, 2016-05-27
@BuriK666

php.net/manual/en/function.usort.php

A
Alexander Aksentiev, 2016-05-27
@Sanasol

++ You don't need to sort anything.
one pass

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question