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.

3
  • How do you form these two arrays? Can you place some PHP code also? Commented Apr 9, 2015 at 7:50
  • Can you paste your code ? Commented Apr 9, 2015 at 7: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 stackoverflow.com/a/17364128/476 and try the methods outlined there. Commented Apr 9, 2015 at 8:02

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Then you did a copy paste error. See the code working: 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().
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);

1 Comment

I am getting Fatal error: Cannot use object of type stdClass as array in
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);
?>

Comments

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));

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.