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:
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);