繁体   English   中英

在PHP中将(多维)数组推送到多维数组

[英]pushing (multidimensional) arrays to multidimensional arrays in php

我正在尝试将我的新闻源结果转换为数组,以便我现在可以对结果进行分组,并输入“Peter和其他6个评论......”等条目。

我想要求其他人,他们通过in_array()评论同一个线程; 将是一个很好的解决方案。 但现在我很难将我的整个mysql结果转换为数组,而不必更改原始while循环的整个结构,其中所有数据库值都是使用$ getnews_row请求的。 我认为我正在创建数组的方式可能存在错误。

我花了几个小时用谷歌搜索我的手,但我找不到任何变量(数组)实际添加到多维数组的例子。

有任何想法吗?

我的PHP:

$x = 0;
while($getnews_row = mysql_fetch_array($res_getnews))
{
$this_getnewsrow = array
  (
  [$x] => array
  ($getnews_row)
  )
  $x++;
}

$x = $x_total;
$x=0;
while($x < $x_total)
{

    $getnews_row =  $this_getnewsrow[$x];

    echo $getnews_row['id'];

    x++;
}

我的输出:

解析错误:第48行的/newsfeed/index.php中的语法错误,意外的'[',期待')'(此摘录中的第6行)

您不能以这种方式初始化数组。

尝试替换它

$x = 0;
while($getnews_row = mysql_fetch_array($res_getnews))
{
$this_getnewsrow = array
  (
  [$x] => array
  ($getnews_row)
  )
  $x++;
}

有了这个

while($getnews_row = mysql_fetch_array($res_getnews))
{
$this_getnewsrow[] = $getnews_row;
}

看一下PHP:Arrays


也尝试替换$x = $x_total; $x_total = $x;

并在第二个循环中添加$x变量。

尝试这个:

$i = 0;
while($getnews_row = mysql_fetch_array ($res_getnews)) {
    $this_getnewsrow [$i] = $getnews_row;
    $i++;
}

$j = 0;
while($j < $i_total)
{

    $getnews_row =  $this_getnewsrow[$j];

    echo $getnews_row ['id'];

    $j++;
}

UPDATE

<?php
while ($row = mysql_fetch_array($res_getnews, MYSQL_ASSOC)) {
    echo $row ['id'];
}

暂无
暂无

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

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