简体   繁体   English

json解码/编码重复数组

[英]json decode/encode repeated array

hey ive managed to decode the encode json i created but when i try to print the decoded array it repeats the same username ( the last one on the list ) over and over again. 嘿,我设法解码了我创建的编码json,但是当我尝试打印解码后的数组时,它一遍又一遍地重复相同的用户名(列表中的最后一个)。 what i want is that all the users are desplayed 我想要的是所有用户都被显示

this is the code for the encoded json array 这是编码的json数组的代码

$query = 
  "SELECT 
  userid, 
  username, 
  password, 
  email 
  FROM Users ORDER BY userid";

$results = mysqli_query($connection,$query);

The encoded array code below 下面的编码数组代码

<?php
echo "Data with Json Encoding";
foreach($results as $row){

    $encode = json_encode($row, true);
    echo '<pre>';print_r($encode); echo '</pre>';
    }
 ?>

the decoded array code below 下面的解码数组代码

<?php
    echo "Data with Json Decoding";
    foreach($results as $row){
    $decode = json_decode($encode, true);
    echo '<pre>'; print_r($decode);'</pre>';
   }

this is the result of the code Data with Json Decoding 这是使用Json Decoding的代码数据的结果

Array
(
    [userid] => 239
    [username] => desposit4221
    [password] => 699e5fae54df4c82314e42dd86c4d383
    [email] => ad471993@hotmail.com
) 

Array
(
    [userid] => 239
    [username] => desposit4221
    [password] => 699e5fae54df4c82314e42dd86c4d383
    [email] => ad471993@hotmail.com
)

it's this over and over again, it should be the list of my users 一遍又一遍,应该是我的用户列表

any help would be greatly appreciated 任何帮助将不胜感激

echo "Data with Json Encoding";
$encodedResults = array();
foreach($results as $row){

    $encode = json_encode($row, true);
    echo '<pre>';print_r($encode); echo '</pre>';

    $encodedResults[] = $encode;
}
 ?>
echo "Data with Json Decoding";
foreach($encodedResults as $row){
    $decode = json_decode($row, true);
    echo '<pre>'; print_r($decode);'</pre>';
}

If you put the line 如果你把线

$encode = json_encode($row, true);

into your second loop just after the foreach line, you should get the results you expect. foreach行之后进入第二个循环,您应该获得期望的结果。 It's a reasonable way to explore the datamanagement aspects of php. 这是探索php数据管理方面的一种合理方法。 Good luck in your projects! 在您的项目中祝您好运!

Encode your array: 编码数组:

echo "Data with Json Encoding";
$encoded_datas = json_encode($results);
echo $encoded_datas;

Decode the string into an array: 将字符串解码为数组:

echo "Data with Json Decoding";
$decoded_datas = json_decode($encoded_datas, true);
echo '<pre>';
print_r($decoded_datas);
echo '</pre>';

You don't need to encode (and then decode) your array row by row. 您不需要逐行编码(然后解码)数组。 Plus, as explained in comments and as shown in costa 's answer, your code didn't work because you were decoding the same string each time: your $encoded variable contains the last value you put in it (the last row you encoded). 另外,如注释中所述和costa的答案所示,您的代码无法正常工作,因为您每次都在解码相同的字符串: $encoded变量包含您输入的最后一个值(编码的最后一行) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM