EDIT: I am trying to merge 2 arrays together, saying that i want to add a new array to the json data without removing the json data that is already in the value.
EXAMPLE: $chatjson = [] has some json data in it which will look like this:
$chatjson = [{"sender":"Testing","message":"Hi"}]
and i want to keep that data when it adds another array so it will look like this
$chatjson = [{"sender":"Testing","message":"Hi"},{"sender":"Testing","message":"Message 2!"}]
Should support my question.
<?php
include '../filter.php';
$chatjson = [];
$sender = SecurePost($_POST["sender"]);
$message = SecurePost($_POST["message"]);
if ($sender || $message) {
$chatarray = array('sender' => $sender, 'message' => $message);
$decodejson = json_decode($chatjson, true);
$merge = array_merge((array)$chatarray, (array)$decodejson);
$chatjson = json_encode($merge);
echo $chatjson;
}
?>
You need to change your code little bit:-
<?php
include '../filter.php';
$chatjson = Array(); // define array like this
$sender = SecurePost($_POST["sender"]);
$message = SecurePost($_POST["message"]);
if ($sender || $message) {
$chatarray[] = array('sender' => $sender, 'message' => $message); // assign values every time to a new index.
$decodejson = json_decode($chatjson, true);
$merge = array_merge((array)$chatarray, (array)$decodejson); // push the new data
$chatjson = json_encode($merge);
echo $chatjson;
}
?>