How to push PHP array inside JSON file with another array
<p>I have a JSON file with an array called "foods". Inside this array I want to push a new object with name, type and price.</p>

<pre><code>{
    "foods":[
        {
            "name": "Name1",
            "tyoe": "Type1",
            "price": "Price1"
        },
        {
            "name": "Name2",
            "type": "Type2",
            "price": "Price2"
        }
    ]
}
</code></pre>

<p>The logic is:<br>
I receive from $_POST a name, type and price. (I'ts a simple form -- it's working ok)<br>
So I want to push these data with PHP inside the "foods" array in JSON file.
The result would be like this:</p>

<pre><code>{
    "foods":[
        {
            "name": "Name1",
            "tyoe": "Type1",
            "price": "Price1"
        },
        {
            "name": "Name2",
            "type": "Type2",
            "price": "Price2"
        },
        {
            "name": "NAME3",
            "type": "TYPE3",
            "price": "PRICE3"
        }
    ]
}
</code></pre>

<p>Wrong example that I'm trying to fix it:</p>

<pre><code>$name = $_POST['name'];
$type = $_POST['type'];
$price = $_POST['price'];

//Do I need to create this array to push inside JSON array "foods"?
$arrayFoods = array(        
    'name' =&gt; $name,
    'type' =&gt; $type,
    'price' =&gt; $price
);

$my_file = file_get_contents('database/file.json');

//Do I need to decode the JSON file to access the array "foods" to push my $arrayFoods? 
//How do I access the "foods" array in JSON file and push my $arrayFoods?
$fileDecode = json_decode($my_file);
</code></pre>

<p>Thanks!</p>