Home > Back-end >  print_r() multidimensional array using PHP while
print_r() multidimensional array using PHP while

Time:10-29

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 "]}";

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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);
  • Related