循环遍历 mysql 查询结果集时填充包含两个元素行的数组

发布于 2025-01-02 21:26:44 字数 837 浏览 0 评论 0原文

我有一个二维数组,我想用 while 循环将值推送给它;

$arr[0][1] = 1. value
$arr[0][2] = 2. value

我已经尝试过

while ($zRow = mysql_fetch_array($zQuery))
{
    $props[]['name'] = $zRow['name'];
    $props[]['photo'] = $zRow['thumbnail'];
}

这个循环将名称推送到 $props[0][name] 并将缩略图推送到 $props[1][photo]

我也尝试过

$j = 0;
while($zRow = mysql_fetch_array($zQuery))
{
    $props[$j]['name'] =$zRow['name'];
    $props[$j]['photo'] =$zRow['thumbnail'];
    $j += 1;
}

这有效,但是有了这个,当我稍后使用 foreach 循环时,它会带来像“非法”这样的麻烦偏移类型”。

这是我的 foreach 循环:

foreach ($props as $no)
{
    echo $props[$no]['name'];
} 

现在我的问题是:

  1. 除了使用 $j 变量(例如用于二维数组的 array_push() )的 while 循环之外,还有其他方法吗?
  2. 如何对二维数组使用 foreach 循环?

I've a 2-dimensional array and i want to push values to it with a while loop like;

$arr[0][1] = 1. value
$arr[0][2] = 2. value

I've tried

while ($zRow = mysql_fetch_array($zQuery))
{
    $props[]['name'] = $zRow['name'];
    $props[]['photo'] = $zRow['thumbnail'];
}

This loop pushes name to $props[0][name] and thumbnail to $props[1][photo]

I also tried

$j = 0;
while($zRow = mysql_fetch_array($zQuery))
{
    $props[$j]['name'] =$zRow['name'];
    $props[$j]['photo'] =$zRow['thumbnail'];
    $j += 1;
}

That works but with this I when I use foreach loop later, it makes trouble like "Illegal offset type".

Here is my foreach loop:

foreach ($props as $no)
{
    echo $props[$no]['name'];
} 

Now my questions:

  1. Are there any other way than while loop with $j variable like array_push() for 2-dimensional arrays?
  2. How can I use foreach loop for 2-dimensional arrays?

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

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

发布评论

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

评论(2

仅此而已 2025-01-09 21:26:44

您可以将第一个循环更改为以下内容:

while($zRow = mysql_fetch_array($zQuery))
{
    $row = array();
    $row['name'] = $zRow['name'];
    $row['photo'] = $zRow['thumbnail'];
    $props[] = $row;
}

您的方法也有效,但您需要额外的变量。

在第二个循环中,您实际需要做的是:

foreach($props as $index => $array)
{
    echo $props[$index]['name'];
    // OR
    echo $array['name'];
}

You could change the first loop to the following:

while($zRow = mysql_fetch_array($zQuery))
{
    $row = array();
    $row['name'] = $zRow['name'];
    $row['photo'] = $zRow['thumbnail'];
    $props[] = $row;
}

Your method also works, but you need that extra variable.

In your second loop, what you actually need to be doing is:

foreach($props as $index => $array)
{
    echo $props[$index]['name'];
    // OR
    echo $array['name'];
}
南城追梦 2025-01-09 21:26:44

使用 $myArray[] = 'foo' 将任何内容推入数组都会增加数组的计数器。

对于多维数组,您需要填充“内部”数组,然后将其推送到“外部”(在您的情况下为$props)数组。

while($zRow = mysql_fetch_array($zQuery)) {
    $data = array('name' => $zRow['name'], 'photo' => $zRow['thumbnail']);
    $props[] = $data;
}

要迭代深度已知的多维数组:

foreach ($props as $prop) {
    foreach ($prop as $key => $value) {
        echo "{$key} => {$value}" . PHP_EOL;
    }
}

如果嵌套深度未知,您可能必须使用递归函数来收集数据。

Pushing anything onto an array with $myArray[] = 'foo' will increment the array's counter.

For multidimensional array, you need to populate the "inner" array, then push it to the "outer" (in your case $props) array.

while($zRow = mysql_fetch_array($zQuery)) {
    $data = array('name' => $zRow['name'], 'photo' => $zRow['thumbnail']);
    $props[] = $data;
}

To iterate over multidimensional arrays whose depth is known:

foreach ($props as $prop) {
    foreach ($prop as $key => $value) {
        echo "{$key} => {$value}" . PHP_EOL;
    }
}

If the depth of the nesting is not known, you may have to use a recursive function to gather the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文