0

I have an array of json as shown below and i want to sort it by size

Array
(
    [0] => {"file_name":"Desert - Copy.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000012","sha256":"010f60d2927a35d0235490136ef9f4953b7ee453073794bcaf153d20a64544ea","size":"845941"}
    [1] => {"file_name":"Hydrangeas - Copy.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000013","sha256":"3b92fede080f9b0ec902afc58831191b5b8ccbaf6732352fd7a8b445d1e9f0bd","size":"595284"}
    [2] => {"file_name":"Tulips.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000014","sha256":"b9352f2565260219db72fc1fc896113a26c85866b69c50d3970c4d9f5cce830a","size":"620888"}
)

Result expected:

Array
(

    [0] => {"file_name":"Hydrangeas - Copy.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000013","sha256":"3b92fede080f9b0ec902afc58831191b5b8ccbaf6732352fd7a8b445d1e9f0bd","size":"595284"}
    [1] => {"file_name":"Tulips.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000014","sha256":"b9352f2565260219db72fc1fc896113a26c85866b69c50d3970c4d9f5cce830a","size":"620888"}
    [2] => {"file_name":"Desert - Copy.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000012","sha256":"010f60d2927a35d0235490136ef9f4953b7ee453073794bcaf153d20a64544ea","size":"845941"}
)

How do i accomplish this in an efficient manner

1

3 Answers 3

0
<?php
 function sortByYear($a, $b) {
 $dA = new DateTime($a['date']);
 $dB = new DateTime($b['date']);
return $dA->format('y') - $dB->format('y');
 }
$data = '{"info":[{"id":1, "title":"original title", "name":"john doe", "date":"2010-05-15"}, {"id":2, "title":"another title", "name":"foo bar", "date":"2009-04-11"}]}';
$d = json_decode($data, true);
$info = $d['info'];
usort($info, 'sortByYear');

print_r($info);
?>

source:http://codepad.org/6N4fmbKG

You should read this for further help:How to sort a JSON array with PHP

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

3 Comments

This answer is a copy/paste from a sort-of similar SO question, and apart from pointing to usort() it doesn't help the OP at all.
Hey. I had posted the code because SO does not encourage users to post link only answers. As links can be dead after some time. And respecting the original question source i have provided the link to the source and the similiar question. :)
If you think this question is the same as the one you linked to (and so deserves an identical answer) then it should be closed as a duplicate, rather than copying the answer.
0

Here is what i came up with but i welcome more efficient solutions

function sort_json($json_array)
    {
        if (count($json_array) > 1) {
            //convert to array
            foreach ($json_array as &$json) {
                $array[] = json_decode($json);

            }
            // sort in ascending order by size
            usort($array, function($a, $b)
            {
                return $a->size < $b->size ? -1 : 1; //Compare the sizes
            });

            //convert back to json
            $json_array = array();
            foreach ($array as &$a) {
                $json_array[] = json_encode($a);
            }
        }
        return $json_array;
    }

Comments

0

Use the following code:

function sortBySize($a, $b)
    {
        if ($a['size'] == $b['size']) return 0;
        return ($a['size'] < $b['size']) ? -1 : 1;

    }

usort($data, "sortBySize");

How usort work : http://www.w3schools.com/php/func_array_usort.asp

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.