【发布时间】:2015-01-11 20:07:53
【问题描述】:
我有一个存储在$t_comments 中的多维数组,看起来像这样:
Array (
[0] => Array
(
[0] => 889
[1] => First comment
[2] => 8128912812
[3] => approved
)
[1] => Array
(
[0] => 201
[1] => This is the second comment
[2] => 333333
[3] => deleted
)
// There is more...
)
目前,我像这样循环遍历数组:
foreach($t_comments as $t_comment) {
echo $t_comment[0]; // id
echo $t_comment[1]; // comment
echo $t_comment[2]; // timestamp
echo $t_comment[3]; // status
}
我想对数组进行 foreach 循环,并且只显示具有值 approved 的数组(如上所示)。 在考虑性能时我该怎么做?
【问题讨论】:
-
你有没有尝试过自己解决问题?
-
我能想到的最接近的方法是如果状态为“已批准”,则执行 if 语句检查每个循环,但在考虑性能时,这似乎是一种不好的方法。
-
实际上我认为关于性能的最佳方法是@HenrikPetterson 建议的。检查这些benchmarks。
标签: php arrays multidimensional-array foreach