PHP array_search()没有返回正确的索引

问题描述:

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("
",$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;

Add four spaces before your search value.