我有以下数组结构: $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
}
while ($row = fetch()) {
if (isset($array[$row['db_id']])) {
foreach ($array[$row['db_id']] as $some_text_value => $some_text_values_value) {
echo ...
}
}
}
foreach ($array as $certain_key => $value) {
if($certain_key == $row['db_id']) {
foreach ($value as $some_text_value) {
echo "$v2'n";
}
}
}
你的意思是像
foreach($array[$certain_key] as $k => $v)
{
do_stuff();
}
?
也许你正在寻找array_key_exists
?它的工作原理是这样的:
if(array_key_exists($certain_key, $array)) {
// do something
}
<?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
}
}