PHP的foreach与多维数组 [英] php foreach with multidimensional array
问题描述
我正在开发一个使用数据库类来查询MySQL中的PHP应用程序。
I'm developing a php app that uses a database class to query mySQL.
类是在这里:<一href=\"http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/\">http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/
我就这一类的一些调整,以适合我的需要,但有一个问题(可能是愚蠢的)
I made some tweaks on the class to fit my needs, but there is a problem (maybe a stupid one)
在使用select()返回一个多维数组一样,对于具有3 COLS表(ID,名字,姓氏):
When using select() it returns a multidimensional array like that for a table that has 3 cols (id, firstname, lastname):
Array
(
[0] => Array
(
[id] => 1
[firstname] => Firstname one
[lastname] => Lastname one
)
[1] => Array
(
[id] => 2
[firstname] => Firstname two
[lastname] => Lastname two
)
[2] => Array
(
[id] => 3
[firstname] => Firstname three
[lastname] => Lastname three
)
)
现在我想这个数组作为MySQL的结果(mysql_fetch_assoc)。
Now I want this array to be used as a mysql result (mysql_fetch_assoc).
我知道,它可能用foreach()中使用,但是这与简单的数组。所以我觉得我必须重新声明一个新的foreach()withing各的foreach(),但我认为这可能减慢或导致一些更高的服务器负载。
I know that it may be used with foreach() but this is with simple arrays. so I think that I have to redeclare a new foreach() withing each foreach(), but I think this could slow down or cause some higher server load.
因此,如何与这个多维数组的foreach适用于()最简单的方法?
So how to apply foreach() with this multidimensional array the simplest way?
感谢
推荐答案
您可以使用的foreach这里就好了。
You can use foreach here just fine.
foreach ($rows as $row) {
echo $row['id'];
echo $row['firstname'];
echo $row['lastname'];
}
我觉得你是用来访问与数字indicies数据(如 $行[0]
),但是这是没有必要的。我们可以使用关联数组来得到我们是以后的数据。
I think you are used to accessing the data with numerical indicies (such as $row[0]
), but this is not necessary. We can use associative arrays to get the data we're after.
这篇关于PHP的foreach与多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!