1

I have a json array (named $ourData) which currently looks something like this:

[
    //this item of $ourData named $officer_0
    {
        "code": "cg",
        "tots": [],
        "pds": []
    }
]

Now if I wanted to push some associative values to tots (something like "date" : "value"), how would I accomplish this?

2 Answers 2

2
$data = json_decode($ourData, true);
$data['tots'][] = 'new data to add';
$ourData = json_encode($data);
Sign up to request clarification or add additional context in comments.

1 Comment

That means that [0] does not exist in the array.
1

here is how you do it solution

$json = '[
    {
        "code": "cg",
        "tots": [],
        "pds": []
    }
]';

$arr = json_decode($json, true);

$arr[0]['tots'][] = array("date" => date('Y-m-d'));

$json = json_encode($arr);

echo $json;

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.