1

I'm trying to sort the a specific node inside a json object and return it in a PHP function that looks like this.

{
    "2007":{
        "number-xx" : "5",
        "number-aa" : "30",
        "number-ef" : "2",
        "number-sa" : "-10",
        "number-ab" : "28",
    },
    "2008":{
        "number-xx" : "-1",
        "number-aa" : "0.5",
        "number-ef" : "23",
        "number-sa" : "55",
        "number-ab" : "43",    
    }
}

I want sort to each node under the "year" descending order and return it back in a function

{
   "number-xx" : "-1",
   "number-aa" : "0.5",
   "number-ef" : "23",
   "number-sa" : "55",
   "number-ab" : "43",    
}

PHP code to return the json

private function buildQuilt($fund_type){
    $path = storage_path() . "/data.json";
    $json = json_decode(file_get_contents($path), true); 

    //Do sort here
    foreach($json as $key => &$arr) {
       natsort($arr);
       $arr = array_reverse($arr);
    }//Seems to be breaking full object?

    return $json;
}

I want to return the entire data.json object sorted but my solution above seems to only return on year node.

2 Answers 2

1

Are you expecting this?

PHP code demo

<?php
ini_set("display_errors", 1);
$json='{
    "2007":{
        "number-xx" : "5",
        "number-aa" : "30",
        "number-ef" : "2",
        "number-sa" : "-10",
        "number-ab" : "28"
    },
    "2008":{
        "number-xx" : "-1",
        "number-aa" : "0.5",
        "number-ef" : "23",
        "number-sa" : "55",
        "number-ab" : "43"  
    }
}';
$data=json_decode($json,true);
$result=array();
foreach($data as $key=>$nodeData)
{
    asort($nodeData);
    $result[$key]=  $nodeData;
}
print_r($result);

Output:

Array
(
    [2007] => Array
        (
            [number-sa] => -10
            [number-ef] => 2
            [number-xx] => 5
            [number-ab] => 28
            [number-aa] => 30
        )

    [2008] => Array
        (
            [number-xx] => -1
            [number-aa] => 0.5
            [number-ef] => 23
            [number-ab] => 43
            [number-sa] => 55
        )

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

6 Comments

no I only want the node inside the years to be sorted. Also your example only sorts the first node.
on which bases you want to sort , on the basis of keys or values?
values only and only the nodes inside the year and NOT the year node itself. Hope that made sense!
@BaconJuice I have updated my post, Can you please check?
Thank you for your help! But the year is still flipped from 2007 being first to 2008 being first in your output. I basically don't want the years to switch only the values inside the year to change.
|
1

you may simply use array_map with this :

private function buildQuilt($fund_type)
{
    $path = storage_path() . "/data.json";
    $json = json_decode(file_get_contents($path), true); 

    return array_map(function ($v) {
        natsort($v);
        return $v;
    }, $json);
}

this will output:

Array (
    [2007] => Array (
        [number-sa] => -10
        [number-ef] => 2
        [number-xx] => 5
        [number-ab] => 28
        [number-aa] => 30
    )
    [2008] => Array (
        [number-xx] => -1
        [number-aa] => 0.5
        [number-ef] => 23
        [number-ab] => 43
        [number-sa] => 55
    )
)

live demo : https://3v4l.org/NHh7p

and if you want to reverse the order of your years keys , https://3v4l.org/YX3Ju


Update

I want sort to each node under the "year" descending order and return it back in a function

to sort your 2-D array descending, you may use rsort instead :

private function buildQuilt($fund_type)
{
    $path = storage_path() . "/data.json";
    $json = json_decode(file_get_contents($path), true); 

    return array_map(function ($v) {
        rsort($v);
        return $v;
    }, $json);
}

this will output :

Array
(
    [2007] => Array
        (
            [0] => 30
            [1] => 28
            [2] => 5
            [3] => 2
            [4] => -10
        )

    [2008] => Array
        (
            [0] => 55
            [1] => 43
            [2] => 23
            [3] => 0.5
            [4] => -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.