从mysql结果Php多维数组

时间:2021-07-29 21:28:43

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

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

3 个解决方案

#1


23  

$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;
}

#2


1  

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 |家长| name | 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.

警告:似乎这可能只适用于两级布局。生病的即兴创作代码,并发布我的代码片段的下一个版本。

#3


1  

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

}

}

#1


23  

$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;
}

#2


1  

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 |家长| name | 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.

警告:似乎这可能只适用于两级布局。生病的即兴创作代码,并发布我的代码片段的下一个版本。

#3


1  

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

}

}