dtntjwkl83750 2010-08-30 00:50
浏览 18
已采纳

PHP搜索数组问题

I need to have an array_search search my array strictly making sure that the item is identical (meaning the whole thing is the same thing as the input value). I know about the third variable in a array_search() function in PHP - the Strict one, but I don't want it to check if its the same instance, as it is not. How would I do this?

Here is the code I'm currently using:

array_search(some, array(someItem,anothervariable, yetanothervariable, some)); 
//output - 0 (the first item) which contains some but isn't exactly some.

Requested output:

Instead of outputting the first item that contains some, someItem, it would output the key for the last item, 3, that is the exact search value.

  • 写回答

3条回答 默认 最新

  • donglu5047 2010-08-30 01:10
    关注

    Are you open to using a foreach loop instead of array_search?

    If so try this:

    $haystack = array('something', 'someone', 'somewhere', 'some');
    $needle = 'some';
    
    foreach($haystack as $key=>$straw){
        if($straw === $needle){
            $straws[$key] = $straw;
            }
        }
    
    print_r($straws);
    

    it will print

    Array ( [3] => some ) 
    

    Or you could use array_keys() with the search value specified.

    Then, using the same $haystack and $needle above:

    $result = array_keys($haystack,$needle,TRUE);
    print_r($result);
    

    This returns:

    Array ( [0] => 3 ) 
    

    The first argument is the array to return keys from, The second arg is the search value, if you include this arg, then only keys from array elements that match the search value are returned. Including the third boolean arg tells the function to use match type as well (===).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大