我的数据来自$myoptions = my_get_option(\'my_repeat_group\');
$p = \'T1X 0L6\'; // find me
print_r($myoptions) =
Array ( [0] => Array (
[region_name] => Alberta
[postal_codes] => T1X 0L3,T1X 0L4,T1X 0L5,T1X 0L6,T1X 0L7
[region_discount] => .5 )
[1] => Array (
[region_name] => Ontario
[postal_codes] => T1M 0W3,T1M 0W4,T1M 0W5,T1M 0W6,T1M 0W7
[region_discount] => .25 ) )
foreach ( $myoptions as $key => $value ) {
if ( in_array($p, $value[\'postal_codes\'] ) {
// need to know $key of which array the $p was found,
// in this case, I would like to find [0]
}
}
我想找到
$p
在内部
$value[\'postal_codes\']
, 当我找到它的时候,把钥匙还给我。
最合适的回答,由SO网友:Liam Bailey 整理而成
$value[\'postal_codes\']
不是array
这是一个string
, 你要么需要像这样分解它:
foreach ( $myoptions as $key => $value ) {
if (in_array($p,explode(","$value[\'postal_codes\']){
// need to know $key of which array the $p was found, in this case I would like to find [0]
}
}
或搜索字符串
foreach ( $myoptions as $key => $value ) {
if (strstr($value[\'postal_codes\'],$p){
}
}
或者,最有效的方法是在分解的对象上使用array\\u search,如下所示:
foreach($myoptions as $key => $options) {
$keys_containing_p[$key] = array_search($p,explode(",",$options[\'postal_codes\']));
}