How to sort json array in php by key?

php sort multidimensional array by key
php sort json array by value
php sort multidimensional array by value alphabetically
php sort array by key
php array sort by column
php usort
php sort descending
php json sort keys

I'm trying to sort json array by key in php. Although I use usort, it's not working. My json array is

$data = [
    {
        "profile_name": "J3S",
        "post_count": 9
    },
    {
        "profile_name": "John",
        "post_count": 3
    },
    {
        "profile_name": "sato",
        "post_count": 10
    }
]

I would like to sort the array by post_count. Here is my code that I tried.

$data = json_encode($data, true);

usort($data, function ($a, $b) {
    return $a['post_count'] <=> $b['post_count'];
});  

Could you please help me?

You can use array_multisort to do this efficiently:

$array = json_decode($data, true);
array_multisort(array_column($array, 'post_count'), SORT_ASC, $array);
echo json_encode($array, JSON_PRETTY_PRINT);

Output:

[
  { "profile_name": "John", "post_count": 3 } 
  { "profile_name": "J3S", "post_count": 9 },
  { "profile_name": "sato", "post_count": 10 },
]

If you want to sort by post_count descending, just change the SORT_ASC to SORT_DESC in the call to array_multisort.

Demo on 3v4l.org

Note this code assumes $data is actually a JSON string. If it is actually the array represented by the JSON in the question, you can skip the json_decode step.

PHP: Sort JSON data by keys, In order to use ksort , you first have to convert the json to PHP array by using: // the true argument specifies that it needs to be converted into a PHP array $array = json_encode($your_json, true); Then apply ksort on that array. And finally json_encode it again to get the result back in json. The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Syntax

You have to decode the json, not encode it:

$data = '[
    {
        "profile_name": "J3S",
        "post_count": 9
    },
    {
        "profile_name": "John",
        "post_count": 3
    },
    {
        "profile_name": "sato",
        "post_count": 10
    }
]';

$data = json_decode($data, true);

usort($data, function ($a, $b) {
    return $a['post_count'] <=> $b['post_count'];
}); 

I asume your json is a string because if it's not that's not valid php.

Output ( var_dump( $data ); ):

array(3) {
  [0]=>
  array(2) {
    ["profile_name"]=>
    string(4) "John"
    ["post_count"]=>
    int(3)
  }
  [1]=>
  array(2) {
    ["profile_name"]=>
    string(3) "J3S"
    ["post_count"]=>
    int(9)
  }
  [2]=>
  array(2) {
    ["profile_name"]=>
    string(4) "sato"
    ["post_count"]=>
    int(10)
  }
}

php sort json array Code Example, Get code examples like "php sort json array" instantly right from your sort multidimensional array by key � php sorting multidimensional array� Sorting function attributes; Function name Sorts by Maintains key association Order of sort Related functions; array_multisort() value: associative yes, numeric no: first array or sort options: array_walk() asort() value: yes: low to high: arsort() arsort() value: yes: high to low: asort() krsort() key: yes: high to low: ksort() ksort() key: yes: low to high: asort() natcasesort() value: yes

You can do it by usort() function.

Add a callback function and compare the profile_count each time.

<?php
$data = [
    ["profile_name"=> "J3S", "post_count"=> 9],
    ["profile_name"=> "John", "post_count"=> 3],
    ["profile_name"=> "sato", "post_count"=> 10]
];
function cmpBySort($a, $b) {
    return $a['post_count'] - $b['post_count'];
}
usort($data, 'cmpBySort');
echo '<pre>';print_r($data);echo '</pre>';

Output:

Array
(
    [0] => Array
        (
            [profile_name] => John
            [post_count] => 3
        )

    [1] => Array
        (
            [profile_name] => J3S
            [post_count] => 9
        )

    [2] => Array
        (
            [profile_name] => sato
            [post_count] => 10
        )

)

How to sort json array value as per key value using PHP , I need to sort my json array as per key value and resize it using php. I am explaining my code below. $skillArr=array(array("type"=>1,"name"=>"aaa")� When using value of this form for array keys use sprintf('%f', 0.00001) to generate the key, for smaller values the precision needs to be included e.g. sprintf('%0.10f', 0.00000001) up

Sorting Arrays - Manual, Some sort based on the array keys, whereas others by the values: $array['key'] = ' value';; Whether or not the correlation between the keys and values are� array_multisort (array &$array1 [, mixed $array1_sort_order = SORT_ASC [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $ ]]]) : bool array_multisort () can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions. Associative (string) keys will be maintained, but numeric keys will be re-indexed.

Sort JSON Object Array Based On A Key Attribute In JavaScript, JSON return type is an array of objects. Hence sort method cannot be used directly to sort the array. However, we can use a comparer function� array_keys() returns the keys, numeric and string, from the array. If a search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

Sort json array by key php Jobs, Employment, Search for jobs related to Sort json array by key php or hire on the world's largest freelancing marketplace with 17m+ jobs. It's free to sign up and bid on jobs. After i use json_encode on this array. The keys are sorted, although i get it as JSON object. PHP itself doesnt seem to sort the array, firefox doesn’t as well

Comments
  • Is $data a JSON string? Because that's not a valid PHP structure.
  • No such thing as a JSON array as JSON is the string representation of array data (and objects). So your sorting on a string. In other words, sort the Array first, then turn it into JSON.
  • Did one of these answers solve your problem? If not, could you provide more information to help answer it? Otherwise, please consider marking the answer which best solved your problem accepted (the check mark under the up/down vote arrows). See stackoverflow.com/help/someone-answers