php 来自mysql结果的Php多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5053857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:39:51  来源:igfitidea点击:

Php multi-dimensional array from mysql result

phpmysqlarraysmultidimensional-array

提问by Levi Self

I have a mysql table that looks like this:

我有一个 mysql 表,看起来像这样:

id | uid | title     | description | parent
1  |  1  | Portraits | desc.       | photostream
2  |  1  | Abstract  | descr.      | photostream

and I am trying to build a multi-dimensional array that would end up looking like this:

我正在尝试构建一个最终看起来像这样的多维数组:

Array
(
      [0]
          [id] => 1
          [uid] => 1
          [title] => Portraits
          [description] => desc.
          [parent] => photostream
      [1]
          [id] => 2
          [uid] => 1
          [title] => Abstract
          [description] => descr.
          [parent] => photostream
)

I am using the select query:

我正在使用选择查询:

$query = mysql_query(
  "SELECT * FROM `table` WHERE `uid`='1' ORDER BY `id` DESC");

Does anyone know how to do this? Thanks, Levi

有谁知道如何做到这一点?谢谢,列维

回答by KeatsKelleher

$query = mysql_query("SELECT * FROM table WHERE uid = '1' ORDER BY id DESC");
$results = array();
while($line = mysql_fetch_array($query, MYSQL_ASSOC)){
    $results[] = $line;
}

回答by CoasterChris

This does not include the sql functions for gathering the data but I made a table for my issue (was the same exact problem) This is my table also by the way. So its more of example than an explanation.

这不包括用于收集数据的 sql 函数,但我为我的问题制作了一个表格(完全相同的问题)顺便说一下,这也是我的表格。所以它更多的是例子而不是解释。

This is not a full on explanation but is a example code.

这不是一个完整的解释,而是一个示例代码。



TABLE:
[ ID | Parent | name |href | title ]

表:
[ ID | 家长 | 名称 |href | 标题 ]



CODE:

代码:

            foreach ($query->result_array() as $row)
{
    if ($row['parent'] === null)
        $data[$row['id']][0]= '<a href="'.$row['href'].'"'.($row['title']!==null)?'title="'.$row['title'].'"':''.'>'.$row['name'].'</a>';
    else 
    {
        $temp = '<a href="'.$row['href'].'"'.($row['title']!==null)?'title="'.$row['title'].'"':''.'>'.$row['name'].'</a>';
        array_push($data[$row['parent']],$temp);
    }
}

I used this to generate my links for my navbar. Then I used a function to make a multi level list out of them. For my issue. Its very similar issue but this was my solution.

我用它来为我的导航栏生成链接。然后我使用一个函数来制作一个多级列表。对于我的问题。它的问题非常相似,但这是我的解决方案。

If you would like. Instead of making your own version. I can make a similar code using your database scheme for the data instead..

如果你愿意。而不是制作自己的版本。我可以使用您的数据库方案为数据制作类似的代码。

Warning:It seems this might only work on a two level layout at moment. Ill improvise code some more and post the next version of my snippet.

警告:目前看来这可能只适用于两层布局。我会即兴编写更多代码并发布我的代码片段的下一个版本。

回答by novice

$query = mysql_query("SELECT * FROM table WHERE uid = '1' ORDER BY id DESC");
$results = array();
$num_fields=mysql_num_fields($query);
$num_rows=mysql_num_rows($query);
while($line = mysql_fetch_array($query)){

for($i=0;$i<$num_rows;$i++
{
  for($j=0;$j<$num_fields;$j++
  {

  $results[$i][$j]=$line[$i][$j];
  }

}

}