腓推值到一个2维阵列 [英] Php pushing values to a 2-dimensional array

查看:119
本文介绍了腓推值到一个2维阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2维数组,我想推值,以它像一个while循环;

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

我试过

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

这个循环推名字$道具[0] [名]和缩略图至$道具[1] [图片]

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

我也试过

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

这本工作,但我当我后来用foreach循环,它使麻烦像非法偏移类型

that works but with this i when i use foreach loop later, it makes trouble like "Illegal offset type"

和这里是我的foreach循环

and here is my foreach loop

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

现在我的问题;
1)是否有任何其他方式比while循环,像array_push $ j变量中的2维数组
2)如何使用foreach循环2维数组

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

推荐答案

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

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

这篇关于腓推值到一个2维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆