简体   繁体   中英

PHP array_search() not returning the proper index

I am trying to find a certain line in a file. The file is http://cpcheats.co/pin-tracker/swf/coffee.flr . I'm using the php function search_array(), but it is not giving me the proper key. This code just returns the first line of the file:

$newurl = file_get_contents("http://cpcheats.co/pin-tracker/swf/coffee.flr");
$array = explode("\n",$newurl);
$key = array_search('triggers_mc.pin_mc.triggerFunction = function () {', $array);
echo $array[$key];

Does anyone know why this is happening, or a fix for this?

It's happening because you are missing the 4 space characters from the beginning of your search term.

$key = array_search('    triggers_mc.pin_mc.triggerFunction = function () {', $array);

works fine.

A possible solution if you don't want to include the spaces in your search term would be to loop through $array and trim the values before calling array_search .

在您的搜索值之前添加四个空格。

Array_search() returns the key for needle if it is found in the array, FALSE otherwise.

(int)FALSE == 0

Something is wrong with your $needle , array_search returns false, thus $key = 0 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM