Problem finding array

I am trying to find values ​​inside an array. This array always starts at 0. Unfortunately, array_search starts the search with the element of array 1. Therefore, the first element is always skipped.

How could I “shift” this array to start from 1, or start searching for an array from 0? The array exits the XML web service, so I cannot rally change the results.

+5
source share
2 answers

See the manual, this can help you: http://www.php.net/manual/en/function.array-search.php

If you are trying to do this, use a key increment of one, you can do:

function my_array_search($needle, $haystack, $strict=false) {
     $key = array_search($needle, $haystack, $strict);
     if (is_integer($key)) $key++;
     return $key;
}
my_array_search($xml_service_array);
+2
source

array_search 1. :

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('blue', $array);  // $key = 0
?>

, , 0.

, == === . array_search 0, , :

// doesn't work when element 0 is matched!
if (false == array_search(...)) { ... }

===, ,

// works, even when element 0 is matched
if (false === array_search(...)) { ... }
+13

All Articles