I have a function (which I found here in the forum) that does a search in the array and returns if it has the value that I search or not, so far so good. Only a need arose, I need to return the position number he found, as it returns only -1, so I know what value was not found. So I would like it to return to me which position of the array it found the value. Since I do not have much experience yet, I could not edit the code to do this. Follow the code.
function search($haystack, $needle, $index = NULL) {
if (is_null($haystack)) {
return -1;
}
$arrayIterator = new \RecursiveArrayIterator($haystack);
$iterator = new \RecursiveIteratorIterator($arrayIterator);
while ($iterator->valid()) {
if (( ( isset($index) and ( $iterator->key() == $index ) ) or ( !isset($index) ) ) and ( $iterator->current() == $needle )) {
return $arrayIterator->key();
}
$iterator->next();
}
return -1;
}
Usage
$arrayBanco = returnBanco();
echo search($arrayBanco, $cnpj, 'CNPJ');
Structure of my array
Array
(
[1] => Array
(
[CNPJ] => 02814497000700
[SERIE] => 1
[NOTA] => 000245924
)
[2] => Array
(
[CNPJ] => 05651966000617
[SERIE] => 1
[NOTA] => 000365158
)
[3] => Array
(
[CNPJ] => 05651966000617
[SERIE] => 1
[NOTA] => 000365645
)
[4] => Array
(
[CNPJ] => 05651966000617
[SERIE] => 1
[NOTA] => 000365946
)
)