0

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 "]}";
2
  • 1
    json_encode in a loop is almost never a good idea. What you want is to construct a valid data structure (array, stdClass or something that implements JsonSerializable), and only call json_encode on that structure once. Commented Oct 28, 2021 at 9:51
  • 1
    Apart from that print_r is hardly the correct function to use here to begin with, why would you ever want to pass JSON_PRETTY_PRINT as second parameter for it ... Commented Oct 28, 2021 at 9:55

3 Answers 3

2

You're doing it wrong. Just create an array, append data from MySQL into it and json_encode the array itself:

$array = [];
while ($rsel_userAct_p = mysqli_fetch_array($xsel_userAct_p, MYSQLI_ASSOC)) {
    $array["user_activity"][] = $rsel_userAct_p;
}
echo json_encode($array, JSON_PRETTY_PRINT);

Or even better:

$array = [
    "user_activity" => mysqli_fetch_all($xsel_userAct_p, MYSQLI_ASSOC)
];
echo json_encode($array, JSON_PRETTY_PRINT);
Sign up to request clarification or add additional context in comments.

Comments

2

Your issue is that you are using print_r in the first place. print_r is meant to print out a very specific format which helps with array structures but has nothing to do with json.

Taken from the print_r documentation

<pre>
<?php
    $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
    print_r ($a);
?>
</pre>

will result in

Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
1 => y
[2] => z
)
)

What you actually want (at least guessing from your code example) is you want to print out a single json object that consists of multiple results from your database, based upon the array result you got before.

So instead of using your current code, you create a proper array structure in the first place and then you print it out with the given json function.

$activities = [
    'user_activity' => []
];

while($rsel_userAct_p = mysqli_fetch_array($xsel_userAct_p, MYSQLI_ASSOC)) {
     $activities['user_activity'][] = $rsel_userAct_p;
}
echo json_encode($activities, JSON_PRETTY_PRINT);

The actual result should look exactly like you intend the result to be, and even works regardless of the amount of entries in your database.

Comments

1

This will make your JSON in correct format:

$json='{"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"
    }]}';
$json=str_replace("}{","},{",$json);

Then you can use :

 echo "<pre>";
 print_R(json_decode($json,true));

to convert JSON to PHP array.

You can use in while like:

    $json = [];
while ($rsel_userAct_p = mysqli_fetch_array($xsel_userAct_p, MYSQLI_ASSOC)) {
    $json["user_activity"][] = $rsel_userAct_p;
}
echo json_encode($json, JSON_PRETTY_PRINT);

3 Comments

How to implement it by using while() in PHP?
I have updated code for while please check at bottom.
Wow, it's really works. thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.