0

I am trying to create a JSON array using php. Every time I post a new array the new one needs to append in front of the old array.

I have my code working however, it creates a strange format like [{},[{},[{}]]].

The format I need for my JSON array to be is [{},{},{}...]

[{"Reg_Date":"28-07-2999","Name":"aaa","Surname":"aaa","VideoPath":"aaa","MyComment":"aaa", "ThumbPath":"aaa", "UserId":"aaa"},
{"Reg_Date":"18-07-2015","Name":"bbb","Surname":"bbb","bbb":"bbb","MyComment":"bbb", "ThumbPath":"bbb", "UserId":"bbb"}]

How can I create my array properly?

php:

$results = array(
    array(
        "Reg_Date" => $Reg_Date, 
        "Name" => $NameUser, 
        "Surname" => $SurnameUser, 
        "VideoPath" => $VideoPath, 
        "MyComment" => $MyComment, 
        "ThumbPath" => $ThumbPath, 
        "UserId" => $UserId
    )
);

$inp = file_get_contents('video_JSON_Test.json');
$arr = json_decode($inp);

array_push($results, $arr);

$fp_login = fopen('video_JSON_Test.json', w);
fwrite($fp_login, json_encode($results));
fclose($fp_login);

print_r($results);
echo $NameUser . $SurnameUser, $MyComment . "\n";
echo json_encode($arr);
3
  • When you do array_push($results, $arr);, you mean to add JSON data into the next index of $results, and not another Array? Commented Nov 2, 2015 at 22:36
  • Yes, sorry another index oh that array Commented Nov 2, 2015 at 22:37
  • Are there multiple arrays in $arr? Maybe you want $results = array_merge($results, $arr); Commented Nov 2, 2015 at 22:38

3 Answers 3

2

Replace:

$arr = json_decode($inp);
array_push($results, $arr);

with:

$arr = json_decode($inp, true);
$results = array_merge($results, $arr);
Sign up to request clarification or add additional context in comments.

4 Comments

i got the error : array_merge(): Argument #2 is not an array
Strange, because in your example you show it as an array inserted in the main one. Could you var_dump($arr) and give the results?
it works just if the JSON file is already there with a list one index. If I delete the JSON file and start from 0 i get the error
Add true to your previous line: json_decode($inp, true) as suggested by others.
0

You create double array (array( array() )) and assign its to $results variable. Next, you push this double array to earlier created array in similar construction. Look:

1 step: array( array() ), and we make array_push() on this array.

2 step: array( array(), array( array() ) ), because our double array land in array.

Solution?

  1. Replace double array with single array and create empty container. So $results = array(); and $resultsContainer = array();. Then you fill $results array with values and use array_push($resultsContainer, $results).

  2. Use array_merge() instead of array_push(). Details: PHP: array_merge. But important is thing: you must have to two arrays. Look at doc for json_decode(): PHP: json_decode. This function can return object instead of array. Use second argument true for associative array.

1 Comment

There is nothing wrong with the double array, and in fact, you maintain the same principle with the suggested $resultsContainer: it will become just as double as soon as it receives its first element. It has to be, because the inner array represents in fact an object, but encoded as an associative array. The last point is indeed correct.
0

To append all of the results from the json file to your new 2d structure, spread the file's array of arrays when pushing. If the file is empty, fallback to an empty array, push nothing.

Code: (Demo)

array_push($results, ...$arr ?: []);

Yes, it is just that simple.

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.