将所选数据显示为数组值而不是字符串

问题描述:

I'm trying to put data from my database into seperate arrays within another array. This works but when I'm trying to fetch the 'user_id' information, it only shows one number so it works like a string. How can I get it to work like an array and get the entire user_id?

$fetch = mysqli_query($con, "SELECT * FROM spotify_userdata");

$return_arr = [];

while ($row = mysqli_fetch_array($fetch, MYSQL_ASSOC)) {
    $return_arr[] = array(
        $row_array['user_id'] = $row['user_id'],
        $row_array['name'] = $row['name'],
        $row_array['artists'] = $row['artists'],
    );
}

$user = json_encode($return_arr[0]);

echo $user[2];

This code returns 1 so it show the third number of the user_id. How can I get it to show the entire user_id like this: 111434343

我正在尝试将数据库中的数据放入另一个数组中的单独数组中。 这有效,但是当我尝试获取'user_id'信息时,它只显示一个数字,因此它就像一个字符串。 我怎样才能让它像数组一样工作并获得整个user_id? p>

  $ fetch = mysqli_query($ con,“SELECT * FROM spotify_userdata”); 
 
 $ return_arr = []; 
 
while($ row = mysqli_fetch_array(  $ fetch,MYSQL_ASSOC)){
 $ return_arr [] = array(
 $ row_array ['user_id'] = $ row ['user_id'],
 $ row_array ['name'] = $ row ['name'  ],
 $ row_array ['artists'] = $ row ['artists'],
); 
} 
 
 $ user = json_encode($ return_arr [0]); 
 
echo $ user [  2]; 
  code>  pre> 
 
 

此代码返回1,因此它显示user_id的第三个数字。 如何让它显示整个user_id如下:111434343 p> div>

You have many things in your code that's wrong:

  1. Remove the last array item's comma
  2. Change

$return_arr = [];

To

$return_arr = array();

3.Add:

$row_array = array() 

at the begginning of all that code

At the end your code must be like this:

$fetch = mysqli_query($con, "SELECT * FROM spotify_userdata");

$row_array = array();

while ($row = mysqli_fetch_array($fetch, MYSQL_ASSOC)) {
    $return_arr = array(
        $row_array['user_id'] = $row['user_id'],
        $row_array['name'] = $row['name'],
        $row_array['artists'] = $row['artists'],
    );
}

$user = json_encode($return_arr[0]);

According to your code you should use:

echo $user['user_id'];

But the real problem is - where is $row_array initialized?!?

And even bigger problem - why use "=" inside array creation in the while ... it seems to me that "=>" would fit better, don't you think?