php 中的多维数组?

发布于 2024-11-28 13:27:50 字数 592 浏览 1 评论 0原文

我在 PHP 中遇到数组问题。那么,让我们看看:我在数据库中有一个表“用户”,其中包含以下字段:“姓名、年龄、评级”。用户数量接近 100。我需要从数据库中获取所有用户,将其发布到数组,并使用 JSON end show 对其进行编码。所以我想,我需要执行以下操作:

  1. 从数据库获取一行。
  2. 将所有数据添加到某些数组字段,例如关联数组。
  3. 将该数组推送到某个数组容器。
  4. 将数组容器编码为 JSON。

但是当我尝试编码时,我只得到数组容器中的最后一个元素。我写了类似的东西:

$arrContainer = array();
$arr = array();
$i = 0;
while($row = getDataFromDB)
{
  arr[$i] = $i;
  arr["name"] = row["name"];
  arr["age"] = row["age"];

  array_push($arrContainer, $arr);
  $i++;
}

JSON.encode($arrContainer);

问题:如何用一些数据制作数组数组?

I have a problem with arrays in PHP. So, lets see: I have a table "Users" in database with such fields: "name, surname, age, rating". The number of users is nearly 100. I need to get all of them from database, post it to array, encode them with JSON end show. So I suppose, I need to do the follows:

  1. Get one row from DB.
  2. Add all data to some array fields like associative array.
  3. Push that array to some array container.
  4. Encode array container to JSON.

But when I try to encode, I get only last element in array container. I writted something like that:

$arrContainer = array();
$arr = array();
$i = 0;
while($row = getDataFromDB)
{
  arr[$i] = $i;
  arr["name"] = row["name"];
  arr["age"] = row["age"];

  array_push($arrContainer, $arr);
  $i++;
}

JSON.encode($arrContainer);

QUESTION: How can I make array of arrays with some data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

原来分手还会想你 2024-12-05 13:27:50
while($row = getDataFromDB)
{
   $arr[]=$row; // add $row as element of $arr array
}

现在你得到 和可以Json编码吗

另外,一些数据库扩展可以自动将所有数据返回到多维数组。
请参阅 PDOStatement::fetchAll 手册

while($row = getDataFromDB)
{
   $arr[]=$row; // add $row as element of $arr array
}

Now you get and can Json encode it

Besides, some database extensions can returns all data to multidimensional array automatically.
See PDOStatement::fetchAll manual

月野兔 2024-12-05 13:27:50

试试这个:

$arr = array();
$i = 0;
while($row = getDataFromDB){
  $arr[$i] = $row;
  $i++;
}
JSON.encode($arr);

try this:

$arr = array();
$i = 0;
while($row = getDataFromDB){
  $arr[$i] = $row;
  $i++;
}
JSON.encode($arr);
半窗疏影 2024-12-05 13:27:50

您可以像 @RiaD 的答案一样创建数组
然后输出json为..

while($row = getDataFromDB) // @RiaD's code
{
   $arr[]=$row; // add $row as element of $arr array
}

//json output
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
header("Cache-Control: no-cache, must-revalidate" ); 
header("Pragma: no-cache" );
header('Content-type: application/json');
echo  json_encode($your_array);

You can create array as in @RiaD's answer
and then output json as ..

while($row = getDataFromDB) // @RiaD's code
{
   $arr[]=$row; // add $row as element of $arr array
}

//json output
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
header("Cache-Control: no-cache, must-revalidate" ); 
header("Pragma: no-cache" );
header('Content-type: application/json');
echo  json_encode($your_array);
分開簡單 2024-12-05 13:27:50

您发布的代码是正确的,但是在以下几行中,您忘记将 $ 放在 $arr 变量之前:

arr[$i] = $i;
arr["name"] = row["name"];
arr["age"] = row["age"];

还有行 arr[$i] = $i; 可能应该类似于 $arr['rowNum'] = $i; 让键与值相同似乎是多余的。

应该说,按照 @RiaD 建议使用 $arrContainer[]=$row; 是获取行数组的更快方法,array_push() 更快捷旨在向数组添加多个值或者要将其视为堆栈。

The code you have posted is correct, however in the following lines you forgot to put $ before the $arr variable:

arr[$i] = $i;
arr["name"] = row["name"];
arr["age"] = row["age"];

Also the line arr[$i] = $i; should probably be something like $arr['rowNum'] = $i; having the key be the same as the value seems redundant.

And it should be said that using $arrContainer[]=$row; as @RiaD suggested is a quicker way to get an array of the row, array_push() is more intended to add multiple values to an array or if you want to treat it as a stack.

心如荒岛 2024-12-05 13:27:50
$allValuesArr = array();
$arr= array();
while($myRow = mysql_fetch_array($result))
{
    $arr["uid"] = $myRow["uid"];
    $arr["name"] = $myRow["name"];
    $arr["surname"] = $myRow["surname"];
    $arr["age"] = $myRow["age"];
    $arr["telephone"] = $myRow["telephone"];
    $arr["prof_id"] = $myRow["prof_id"];
    //$allValuesArr[] = $arr;
    array_push($allValuesArr, $arr);
}

$strJSON = json_encode($allValuesArr);
echo $strJSON;

我得到的回应:

[{"uid":"1","name":"Sergii","surname":"Surname1","age":"26","telephone":"0976543135","prof_id":"1225423"},{"uid":"2","name":"Slava","surname":"Surname2","age":"24","telephone":"06353524","prof_id":"13584384"},{"uid":"3","name":"Desame","surname":"Surname3","age":"28","telephone":"0973153584","prof_id":"35843815"}]
$allValuesArr = array();
$arr= array();
while($myRow = mysql_fetch_array($result))
{
    $arr["uid"] = $myRow["uid"];
    $arr["name"] = $myRow["name"];
    $arr["surname"] = $myRow["surname"];
    $arr["age"] = $myRow["age"];
    $arr["telephone"] = $myRow["telephone"];
    $arr["prof_id"] = $myRow["prof_id"];
    //$allValuesArr[] = $arr;
    array_push($allValuesArr, $arr);
}

$strJSON = json_encode($allValuesArr);
echo $strJSON;

response I get:

[{"uid":"1","name":"Sergii","surname":"Surname1","age":"26","telephone":"0976543135","prof_id":"1225423"},{"uid":"2","name":"Slava","surname":"Surname2","age":"24","telephone":"06353524","prof_id":"13584384"},{"uid":"3","name":"Desame","surname":"Surname3","age":"28","telephone":"0973153584","prof_id":"35843815"}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文