Add an new JSON array into an existing JSON array
Add an new JSON array into an existing JSON array
(OP)
I want to add a new JSON array to an existing JSON array that have the same keys. I have searched, but cannot find a solution to match.
My current array
The goal array
I have tried array_merge and array + array, but none result in what I am trying to accomplish. Here is my code:
My current array
CODE -->
[{"meetdate":"2019-06-05","acronym":"dm","type":"_ho_6c"}, {"meetdate":"2019-07-22","acronym":"htn","type":"_ho_6c"} ]
The goal array
CODE -->
[{"meetdate":"2019-06-05","acronym":"dm","type":"_ho_6c"}, {"meetdate":"2019-07-22","acronym":"htn","type":"_ho_6c"}, {"meetdate":"2019-08-15","acronym":"mi","type":"_ho_3c"} ]
I have tried array_merge and array + array, but none result in what I am trying to accomplish. Here is my code:
CODE --> PHP
$ho_meetdate = '2019-08-15'; $ho_acronym = 'mi'; $ho_type = '_ho_3c'; $arr2 = array(); $arr['meetdate'] = $ho_meetdate1; $arr['acronym'] = $ho_acronym1; $arr['type'] = $ho_type1; //get existing array and decode $fileContents = file_get_contents('../../editor/textfiles/handout_arrayTest.txt'); $arr1 = json_decode($fileContents, true); //attempts at adding an array existing array $result = array_merge($arr1, $arr2); $result = $arr1 + $arr2; //Save the array back to a the text file. $ho_encoded = json_encode($result); file_put_contents("textfiles/handout_arrayTest.txt", $ho_encoded);
RE: Add an new JSON array into an existing JSON array
Would coloring the variable names help ?
CODE --> PHP ( fragment )
Regarding the actual array operation, you probably want this one :
CODE --> PHP ( fragment )
Feherke.
feherke.github.io
RE: Add an new JSON array into an existing JSON array
CODE --> Result
{"meetdate":"2019-08-15","acronym":"mi","type":"_ho_3c","0":{"meetdate":"2019-08-15","acronym":"mi","type":"_ho_3c"}}
I finally found an example that I could massage to work. I probably did not explain the problem appropriately.
CODE --> PHP
RE: Add an new JSON array into an existing JSON array
Now this is weird. Here is what I got :
CODE --> command line
Anyway, glad you solved it.
Feherke.
feherke.github.io
RE: Add an new JSON array into an existing JSON array
RE: Add an new JSON array into an existing JSON array
The example that I found and used worked when merging 2 arrays. When I added a third and every array after that, the code added an additional [ bracket at the beginning of the array and a ] bracket at the end of the newly added array.
I retried feherke's example and it worked perfectly. I believe, in my original attempt, I may have had a extra closing curly bracket which affected how the array was added.
Again thanks.
RE: Add an new JSON array into an existing JSON array
Besides, you can always extend an array by assigning to a non named element, which means "append at the end as a new element":
CODE
Since you want $arr2 as a new row in $arr1 and rows in $arr1 consist of an array each, that's wanting to add $arr2 as new element and this code does exactly that. Untested, but in this case you neither need + nor array_merge() to add a new array as a new element of the old. Notice, this does not overwrite the whole array $arr1 and replaces it with $arr2, notice the brackets at the end, this tells PHP, add a new element at the end of the array.
Bye, Olaf.
Olaf Doschke Software Engineering
https://www.doschke.name
RE: Add an new JSON array into an existing JSON array
I am not using the line $result = $arr1 + $arr2, since I could not get it to work. I switched to what feherke recommended: array_merge().
I tried your solution and it worked. Thanks for the bracket explanation, many examples do not explain what each part does.