When i try to add a JSON Array, it removes the previous array. (PHP) -


edit: trying merge 2 arrays together, saying want add new array json data without removing json data in value.

example: $chatjson = [] has json data in this:

$chatjson = [{"sender":"testing","message":"hi"}]  

and want keep data when adds array

$chatjson = [{"sender":"testing","message":"hi"},{"sender":"testing","message":"message 2!"}] 

should support 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 change code little bit:-

<?php    include '../filter.php';   $chatjson = array(); // define array   $sender = securepost($_post["sender"]);   $message = securepost($_post["message"]);    if ($sender || $message) {       $chatarray[] = array('sender' => $sender, 'message' => $message); // assign values every time new index.       $decodejson = json_decode($chatjson, true);        $merge = array_merge((array)$chatarray, (array)$decodejson); // push new data       $chatjson = json_encode($merge);       echo $chatjson;   } ?> 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -