2

I have two JSON encoded arrays 1st Array {
"User_id":2, "Seeds":["11","22","31","14"] }

2nd Array

{
"Seeds": [
    {
        "Team_name": "Belmont Bruins",
        "Team_id": "22",

    },
    {
        "Team_name": "Arkansas State Red Wolves",
        "Team_id": "14",
    },
    {
        "Team_name": "Arizona Wildcats",
        "Team_id": "11",
    },
    {
        "Team_name": "Brown Bears",
        "Team_id": "31",
    }
]

}

Now i need to sort the 2nd array based on the 1st array. The 'Seeds' in the first array corresponds to 'Team_id' in the second array. The output required is:

{
    "Seeds": [
        {
            "Team_name": "Arizona Wildcats",
            "Team_id": "11",

        },
        {
            "Team_name": "Belmont Bruins",
            "Team_id": "22",
        },
        {
            "Team_name": "Brown Bears",
            "Team_id": "31",
        },
        {
            "Team_name": "Arkansas State Red Wolves",
            "Team_id": "14",
        }
    ]
}

I have found similar questions. But the solutions dosn't seem to work in this case.

Abey
  • 1,408
  • 11
  • 26
  • How do you form these two arrays? Can you place some PHP code also? – Anto S Apr 09 '15 at 07:50
  • Can you paste your code ? – US-1234 Apr 09 '15 at 07:54
  • JSON doesn't matter, you simply decode the JSON to arrays and then encode it back afterwards; you can't sort "JSON" because it's just a string. Once you take that out of the equation, look at http://stackoverflow.com/a/17364128/476 and try the methods outlined there. – deceze Apr 09 '15 at 08:02

4 Answers4

1

You should decode json format, sort array and then encode to json format:

$array1 = json_decode($array1_json);
$array2 = json_decode($array2_json);    
foreach ($array1['Seeds'] as $order)
    {
        foreach ($array2['Seeds'] as $data)
        {
            if ($data['Team_id'] == $order)
                $array_sorted[] = $data;
        }
    }
    $array2_sorted = array('Seeds' => $array_sorted);
    echo json_encode($array2_sorted);
Serge
  • 417
  • 2
  • 12
1
<?php

    $sort_by = [11, 22, 31, 14];
    $to_sort = [
            array(
                "Team_name" => "Belmont Bruins",
                "Team_id"=> 22,
            ),
            array(
                "Team_name" => "Arkansas State Red Wolves",
                "Team_id" => 14,
            ),
            array(
                "Team_name" => "Arizona Wildcats",
                "Team_id" => 11,
            ),
            array(
                    "Team_name" => "Brown Bears",
                    "Team_id" => 31,
            )
    ];

    $hash = [];

    foreach ($to_sort as $value){
            $hash[$value["Team_id"]] = $value;
    }

    $len = count($sort_by);
    $sorted = [];

    for ($i=0; $i<$len; $i++)
            $sorted[] = $hash[$sort_by[$i]];

    var_dump($sorted);
?>
vlungu
  • 26
  • 1
1

You can use the following example. I create a lookup using array_flip() and sort the second array based on that.

Having this json:

$j1 = <<<EOF
{
   "User_id":2,
   "Seeds":["11","22","31","14"]
}
EOF;

// I needed to remove addtional commas here
$j2 = <<<EOF
{
"Seeds": [
    {
        "Team_name": "Belmont Bruins",
        "Team_id": "22"

    },
    {
        "Team_name": "Arkansas State Red Wolves",
        "Team_id": "14"
    },
    {
        "Team_name": "Arizona Wildcats",
        "Team_id": "11"
    },
    {
        "Team_name": "Brown Bears",
        "Team_id": "31"
    }
]
}
EOF;

... you can do the following:

// Convert to array
$a1 = json_decode($j1, true);
$a2 = json_decode($j2, true);

// Create lookup
$lookup = array_flip($a1["Seeds"]);

// sort array2
usort($a2["Seeds"], function($a, $b) use($lookup) {
    if($lookup[$a["Team_id"]] > $lookup[$b["Team_id"]]) {
        return 1;
    } else if ($lookup[$a["Team_id"]] < $lookup[$b["Team_id"]]) {
        return -1;
    }
    return 0;
});

// Done
var_dump($a2);
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Then you did a copy paste error. See the code working: http://3v4l.org/YU8hN . It works for all version from PHP 5.3.0 . Note that I'm passing `true` as the second argument to `json_decode()`. – hek2mgl Apr 09 '15 at 08:38
0

You can do something like this :)

$a1 = json_decode('{
   "User_id":2, 
   "Seeds":["11","22","31","14"]
}', true);

$a2 = json_decode('{"Seeds": [
    {
        "Team_name": "Belmont Bruins",
        "Team_id": "22"
    },
    {
        "Team_name": "Arkansas State Red Wolves",
        "Team_id": "14"
    },
    {
        "Team_name": "Arizona Wildcats",
        "Team_id": "11"
    },
    {
        "Team_name": "Brown Bears",
        "Team_id": "31"
    }]}', true);    

$hash = array();
$out = array();
foreach ($a2['Seeds'] as $props) $hash[$props['Team_id']] = $props;
foreach ($a1['Seeds'] as $id) $out[] = $hash[$id];
die(json_encode($out));
mathieu
  • 477
  • 3
  • 9