I have a multidimensional array and I want it printed like these by using PHP, but there is no comma separating the curly braces }{ and it should be like },{. Can you guys help me?
{"user_activity": [{
"log_number": "1",
"log_user_1": "w120511891",
"log_activity_id": "A0002DOC",
"log_user_2": "",
"log_document_id": "DSX00012",
"log_material_id": "",
"log_timestamp": "2021-10-23 13:52:35",
"log_rand_key": "127",
"log_hash_key": "09c7e3bb5d6f74c257aa4b4cdae388a69177c7dc",
"log_project_id": "1520002",
"log_number_reference": "",
"log_close": "1"
}{
"log_number": "9",
"log_user_1": "W201005911",
"log_activity_id": "A0004DOC",
"log_user_2": "",
"log_document_id": "DSX00012",
"log_material_id": "",
"log_timestamp": "2021-10-25 10:35:29",
"log_rand_key": "127",
"log_hash_key": "d04e8d1ef5c9f8b85a3f7556b92d6a7fcdc11639",
"log_project_id": "1520002",
"log_number_reference": "1",
"log_close": "1"
}]}
This is my PHP Code
echo "{\"user_activity\": [";
while($rsel_userAct_p = mysqli_fetch_array($xsel_userAct_p, MYSQLI_ASSOC)) {
print_r(json_encode($rsel_userAct_p), JSON_PRETTY_PRINT);
}
echo "]}";
json_encodein a loop is almost never a good idea. What you want is to construct a valid data structure (array,stdClassor something that implementsJsonSerializable), and only calljson_encodeon that structure once.print_ris hardly the correct function to use here to begin with, why would you ever want to passJSON_PRETTY_PRINTas second parameter for it ...