1

我有以下数组构造:$array[$certain_key][some_text_value]

在一个while循环中,我想打印数组中的数据,其中$certain_key是一个特定的值。

我知道如何循环遍历多维数组,这不是这个问题的完整解决方案:

foreach ($a as $v1) {
    foreach ($v1 as $v2) {
        echo "$v2\n";
    }
}

我不想每次都循环整个数组,但只有在$certain_key匹配时才循环。

编辑:更准确地说,这就是我想要做的:

$array[$array_key][some_text];

while reading from db {

  //print array where a value returned from the db = $array_key

}
4

5 回答 5

2
while ($row = fetch()) {
   if (isset($array[$row['db_id']])) {
      foreach ($array[$row['db_id']] as $some_text_value => $some_text_values_value) {
         echo ...
      }
   }
}
于 2012-04-20T21:21:35.517 回答
1
foreach ($array as $certain_key => $value) {
    if($certain_key == $row['db_id']) {
        foreach ($value as $some_text_value) {
            echo "$v2\n";
        }
    }
}
于 2012-04-20T21:21:56.113 回答
1

你的意思是喜欢

foreach($array[$certain_key] as $k => $v)
{
     do_stuff();
}

?

于 2012-04-20T21:22:21.450 回答
0

也许你正在寻找array_key_exists?它是这样工作的:

if(array_key_exists($certain_key, $array)) {
   // do something
}
于 2012-04-20T21:21:23.307 回答
0
<?php

foreach ($a as $idx => $value) {
    // replace [search_value] with whatever key you are looking for
    if ('[search_value]' == $idx) {
        // the key you are looking for is stored as $idx
        // the row you are looking for is stored as $value
    }
}
于 2012-04-20T21:23:38.570 回答