1
I have a function (which I found here in the forum) that does a search in the array and returns whether it has the value I seek or not, so far so good. Only that a need has arisen, I need to return the number of the position he found, because he only returns -1, so I know what value was not found. Then I would like it to return to me which position of the array it found the value. As I don’t have much experience yet, I couldn’t 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;
}
Utilizing
$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
)
)
I edited there again, take a look!
– LocalHost