【问题标题】:PHP: Finding array index number?PHP:查找数组索引号?
【发布时间】:2011-06-16 04:03:37
【问题描述】:

适合任何真正想要挑战的人! :)

如何使用Wonder Bingo = 0 等字符串查找eirepanel_inline_ads_options_name 的索引

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"
  }
}

【问题讨论】:

  • Wonder Bingo = 0 是什么意思?字符串"Wonder Bingo" 应该在您搜索的键值的0 索引处?另外,你有没有尝试过?

标签: php arrays search


【解决方案1】:
$str = preg_replace('/ = \d+$/', '', 'Wonder Bingo = 0');

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

【讨论】:

    【解决方案2】:

    抱歉各位,刚刚才弄明白。

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

    【讨论】:

      【解决方案3】:

      将要求解释为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;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多