user2638842
user2638842

Reputation: 93

How to add data in JSON using PHP

I have a json file with the following syntax:

[
  {
    "fields": {
      "service_number": "service_number",
      "physical_address": "physical_address",
      "account_id": "account_id",
      "contact_id": "contact_id"
    },
    "someId": "asd23f",
    "status": "Active",
    "perCode": "1",
    "idCode": "0987",
    "nextCode": "09"
  },
  {
    "fields": {
      "service_number": "service_number",
      "physical_address": "physical_address",
      "account_id": "account_id",
      "contact_id": "contact_id"
    },
    "someId": "789096",
    "status": "Active",
    "perCode": "1",
    "idCode": "076543",
    "nextCode": "09"
  }
]

I would like to use a for loop in order to add something like a userID before or after the nextCode. Is there any solution to this? So far, I tried this:

$data = json_decode($file, true);
foreach ($data as $key => $val)
{
    foreach ($val as $key=>$c)
    {
        if (is_array($c))
            continue;
        $data .= $data . "user_id:$userId";
    }
}

Of course it does not make the trick, any ideas please?

Upvotes: 4

Views: 106

Answers (5)

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

Another way which is slightly shorter is pass the value by reference &:

$data = json_decode($file, true);
foreach ($data as &$val) {
    $val['user_id'] = $userId;
}

https://3v4l.org/ZF4Ve

Upvotes: 3

Rafael
Rafael

Reputation: 1535

I see everybody answered for array options. I don't see why not using object, though.

I would do it like this:

$data = json_decode($file);
foreach ($data as &$field)
{
    $field->userID = $userId;
}

$data = json_encode($data);

Upvotes: 1

Christian Cruz
Christian Cruz

Reputation: 668

I explain to you

The following expression converts the information from string to array

$data = json_decode ($file, true);

So in your foreach you only have to add the key

$data = json_decode($file, true);
foreach ($data as $key => $val)
{
    foreach ($val as $k=>$c)
    {
        if (is_array($c))
            continue;
        $data[$k]['user_id'] = $userId;
    }
}

Upvotes: 2

Zhulkov
Zhulkov

Reputation: 106

This is how you can add an element to a parsed JSON and recostruct it back into a JSON:

// parse the JSON into an array
$data = json_decode($file, true);

foreach ($data as $key => $val)
{
    // add the userID to each element of the main array
    // it will be inserted after the last element (after nextCode)
    $data[$key]['userID'] = 'some-id';
}

// if needed, parse the array back to JSON
$data = json_encode($data);

Upvotes: 3

Don't Panic
Don't Panic

Reputation: 41820

There are a few problems.

  • First, the foreach loop works with a copy of the array it's iterating, so modifying one of the items there won't change the original array.

  • Then, your inner foreach loop is overwriting the $key from the outer loop. That would cause problems, but it's okay, because you don't actually need the inner loop.

  • Finally, after you've decoded the JSON string, $data will be an array, so appending to it with .= won't work, and even if it did, you'd just be sticking something on the end of it rather than at the specific point where your loop is.

Just refer to the specific key you need and set the value there.

$data = json_decode($file, true);
foreach ($data as $key => $val)
{
    $data[$key]['user_id'] = $userId;
}
$data = json_encode($data);

Upvotes: 5

Related Questions