Problem with recursive function in array

Asked

Viewed 292 times

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!

2 answers

2


You can use the function array_search to return the array key value:

$key = array_search('conteudo a ser buscado', $array);

Following is php reference: http://php.net/manual/en/function.array-search.php

Edition: In this Multidimensional case, you can do it this way:

<?php
function pesquisarCNPJ($array,$CNPJ){
$i=1;
foreach($array as $valores){
    if($valores['CNPJ']==$CNPJ){
    return $i;
    }
$i++;
}
}
if(pesquisarCNPJ($array,"05651966000617")){
echo "CNPJ encontrado na posição".pesquisarCNPJ($array,"05651966000617");
}
else {
echo "Não encontrado";
}


?>

Or even using php’s own function:

echo $key = array_search($CNPJ, array_column($array, 'CNPJ'));

And in case, if there is more ed a cnpj, just save the indices found in a new array, ai Voce will have all the positions that exist the CNPJ

I hope I’ve helped

  • It seems that this function does not work for multidimensional arrays ..

  • how is the structure of your array?

  • I’ll edit my answer and put the array there.

  • Put instead of Return -1, poe return $iterator->key(); And see if it works

  • It didn’t work, it just stops returning the arrays whose values don’t match my search parameter

  • @Marloncastro may have more than one CNPJ equal?

  • I was at lunch.. Yes you can. I even thought I’d do it that way you, but I’m worried about performace

  • As for performance, there will be no differences, or at least no significant differences, since its function also uses loop repetition, if that is its fear. As long as it’s not an array with thousands of records, everything ok kk

  • kkk.. Yeah, I’ve got your function a little bit so I can meet here. It worked, but I’m still worried about performance kk. But anyway, thanks for your help.

Show 4 more comments

2

See if it works for you:

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 array('interator'=>$arrayIterator->key(),'position'=>$index);
   }

   $iterator->next();
 }
     return -1;
}
  • He returns to me the void in position. Array&#xA;(&#xA; [interator] => 2&#xA; [position] => &#xA;)

  • Although analyzing your code well, the $iterator->key() itself is already the position that the index is at that time

  • So, I thought that would work, but he does not return the position that he found each value. I will edit my answer and put my array there.

  • Do the following, create a variable called $Return[] = array('interator'=>$arrayIterator->key(),'position'=>$index);

  • Didn’t work out..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.