1

I have a multidimensional JSON array that I need to add values to. The JSON array is external and I cannot change its format.

I've tried doing 3 foreach loops, but then I get myself lost in how to add data to the array. I keep catching myself stuck in a loop.

Here's the JSON:

{
    "positions": [{
        "report_at": "2017-03-13 20:04:10",
        "elev": "0",
        "dir": "0",
        "id": "1"
    }, {
        "report_at": "2017-03-07 00:28:14",
        "elev": "1240",
        "dir": "89",
        "id": "2"
    }]
}

I have unique data I need to add to id 1, and another set of unique data I need to add to id 2.

Here's what I've tried:

$data = json_decode( $result, true );

foreach ( $data as $d ) {
    foreach ( $d as $key => $data ) {
        if ( $data['id'] == '1' ) {
            $data[] = array(
                "online_status" => "1",
                "name" => "Test User",
            );
        } elseif ( $data['id'] == '2' ) {
            $data[] = array(
                "online_status" => "0",
                "name" => "Another User",
            );
        }
    }
}

$json = json_encode( $data );
echo $json;

I think once I can get this figured out, then I can pull data from MySQL, build small arrays based off that data, then add those to these sub-arrays where the ID matches the SQL ID. Any help is appreciated.

1
  • 1
    Have you tried using $data = array_merge( $data, array(....) ) instead of the $data[] assignment? Commented Mar 14, 2017 at 21:45

2 Answers 2

1

JSON seems to be just object with "positions" field which is array, you need to modify.

$data = json_decode($json, TRUE);

foreach ($data['positions'] as &$userInfo) {
    if ($userInfo['id'] == 1) {
          $userInfo['online_status'] = 'offline';
          $userInfo['name'] = 'Test user';
    }
}

echo json_encode($data);

Notice "&" sign in foreach, which means, that modification made within foreach, will be stored to original array.

Also you should be aware of key=>value naming in foreach. Your second foreach creates variable named $data, which means, that you are loosing pointer to original array!

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

1 Comment

This works great and adds the items inline with the existing data. Thank you for the explanation of $data, too! Thanks!
1

Use the following approach:

$data = json_decode($result, true);
    foreach ($data['positions'] as &$item) {
    if ($item['id'] == "1") {
        $item = array_merge($item, ["online_status" => "1", "name" => "Test User"]);
    } else if ($item['id'] == "2") {
        $item = array_merge($item, ["online_status" => "0", "name" => "Another User"]);
    }
}

$json = json_encode($data, JSON_PRETTY_PRINT);
echo $json;

The output:

{
    "positions": [
        {
            "report_at": "2017-03-13 20:04:10",
            "elev": "0",
            "dir": "0",
            "id": "1",
            "online_status": "1",
            "name": "Test User"
        },
        {
            "report_at": "2017-03-07 00:28:14",
            "elev": "1240",
            "dir": "89",
            "id": "2",
            "online_status": "0",
            "name": "Another User"
        }
    ]
}

1 Comment

This works great, but the other answer was a little more in line with what I was going after with data inline and not another nested array. Thank you for taking the time to answer it!

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.