1

How can I sort label in order:

1080p
720p
480p
360p


I've tried all Sorting Arrays in PHP documentation and some another "User Defined" sorting functions, but no luck, it shows completely random, not in order.

My original json:

[{"file":"***","label":"360p","default":"true","type":"video\/mp4"},
{"file":"***","label":"720p","type":"video\/mp4"},
{"file":"***","label":"1080p","type":"video\/mp4"},
{"file":"***","label":"480p","type":"video\/mp4"}]
2
  • Have you tried removing the "p" and then sorting? Commented Jun 6, 2017 at 7:24
  • Remove "p" from the element with explode() of each label of the array, then use intval() function to get int value of the string. Then sort by depending index Commented Jun 6, 2017 at 7:27

3 Answers 3

2

This looks pretty straight forward to me, you use a custom comparison function for the sorting:

<?php
$data = json_decode(<<<JSON
[{"file":"***","label":"360p","default":"true","type":"video\/mp4"},
{"file":"***","label":"720p","type":"video\/mp4"},
{"file":"***","label":"1080p","type":"video\/mp4"},
{"file":"***","label":"480p","type":"video\/mp4"}]
JSON
);

usort($data, function($a, $b) {
  return intval($a->label) < intval($b->label);
});
print_r($data);

The output obviously is:

Array
(
    [0] => stdClass Object
        (
            [file] => ***
            [label] => 1080p
            [type] => video/mp4
        )

    [1] => stdClass Object
        (
            [file] => ***
            [label] => 720p
            [type] => video/mp4
        )

    [2] => stdClass Object
        (
            [file] => ***
            [label] => 480p
            [type] => video/mp4
        )

    [3] => stdClass Object
        (
            [file] => ***
            [label] => 360p
            [default] => true
            [type] => video/mp4
        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Really thanks for your help, but Sahil's answer already solved my problem.
@ScottWillsleans You should use this method in your project. There is no need to declare/overwrite variables on each iteration of usort() This is the more concise solution.
1

Here we are using usort to achieve desired output.

Try this code snippet here

<?php

ini_set('display_errors', 1);
$json='[{"file":"***","label":"360p","default":"true","type":"video\/mp4"},
{"file":"***","label":"720p","type":"video\/mp4"},
{"file":"***","label":"1080p","type":"video\/mp4"},
{"file":"***","label":"480p","type":"video\/mp4"}]';
$array=json_decode($json,true);

usort($array, function($value1,$value2){
    $value1["label"]=rtrim($value1["label"],"p");
    $value2["label"]=rtrim($value2["label"],"p");

    return $value1["label"]<$value2["label"];
});

Comments

-1

First you must decode the JSON string using json_decode.

Second sort the decoded array using usort.

<?php
$array = json_decode($json);
usort($array, function ($a, $b) {
    $valueA = (int) $a->label;
    $valueB = (int) $b->label;
    if ($valueA == $valueB) {
        return 0;
    }
    return ($valueA < $valueB) ? -1 : 1;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.