PHP:查找数组索引号?

时间:2011-06-16 04:03:38

标签: php arrays search

对于任何真正想要挑战的人! :)

如何使用eirepanel_inline_ads_options_name

等字符串查找Wonder Bingo = 0的索引?
array(4) {
  ["eirepanel_inline_ads_options_saved"]=>
  string(4) "true"
  ["eirepanel_inline_ads_options_name"]=>
  array(2) {
    [0]=>
    string(12) "Wonder Bingo"
    [1]=>
    string(10) "City Bingo"
  }
  ["eirepanel_inline_ads_options_affiliate_link"]=>
  array(2) {
    [0]=>
    string(20) "/visit/wonder_bingo/"
    [1]=>
    string(17) "/visit/city_bingo"
  }
  ["eirepanel_inline_ads_options_thumbnail"]=>
  array(2) {
    [0]=>
    string(45) "http://www.freenodepositbingo.co.uk/image.png"
    [1]=>
    string(46) "http://www.freenodepositbingo.co.uk/image2.png"
  }
}

3 个答案:

答案 0 :(得分:0)

$str = preg_replace('/ = \d+$/', '', 'Wonder Bingo = 0');

$key = array_search($str, $array['eirepanel_inline_ads_options_name']);

答案 1 :(得分:0)

对不起,伙计们,只是想通了。

echo $key = array_search('WonderBingo', $eirepanel_inline_ads_options['eirepanel_inline_ads_options_name']); 

答案 2 :(得分:0)

将要求解释为Wonder Bingo = 0,意味着搜索到的关键字的0索引等于"Wonder Bingo"

function find($needle, $haystack) {
    preg_match('/^(.+) = (\d+)$/', $needle, $matches);
    foreach ($haystack as $key => $value) {
        if (isset($value[$matches[2]]) && $value[$matches[2]] == $matches[1]) {
            return $key;
        }
    }
}