Retrieve and separate data that is not in the array

Asked

Viewed 50 times

0

I know that using in_array, i check if the value exists in the array.

I need to know if the id, contains numbers from 1 to 6.

If any number between 1 and 6 exists, displays the message, ID existe no array, otherwise I need to separate and store in variables the IDsthat were not found.

public function atualizar_menu($dados)
{
    $array = (array)$dados;
    foreach ($dados as $mnu){
        $menu = array(
            'id'            => $mnu->id,
            'permissao_id'  => $mnu->permissao_id,
            'padrao'        => $mnu->padrao,                
        );
        print_r($menu);

        for ($i = 1; $i <= 6; $i++) {
            if(in_array($i, $menu))
            {
                echo 'ID '.$i.' existe no Array.<br>';
            }
        }
    }//Fim do foreach
}
  • Pretty confusing your code. The value of $dados is a array of objects and you wonder if in any of these objects there is a id between 1 and 6? If so, there is a much simpler way to do this.

  • Besides knowing if there is an ID between 1 and 6 in the array, I want to get the ID that is not in the array. Example: 1, 2 e 3 estão no array e 4,5 e 6 não estão.

1 answer

0

(...)
for ($i = 1; $i <= 6; $i++) {
  if(in_array($i, $menu)){
    echo 'ID '.$i.' existe no Array.<br>';

    if($menu->id >= 1 && $menu->id <= 6){
      //Descarrega o código aqui! =D
    }
  }
}

(..)

  • Thanks for the identation! = D I’m new here, I’m not used to it yet...

  • But it didn’t work either. Message: Undefined offset: 1 E thus, I cannot know which ID is not in the array.

  • It was evil again, put inside the if you verify the existence

  • This way offset error

  • exactly where? ...

  • Right here: if($menu[$i] -> id >= 1 && $menu[$i] -> id <= 6){

  • it doesn’t have a $menu array so it’s just $menu->id, misconceptions... :x

  • Yes, but it’s inside the foreach $mnu->id. What I wanted was to know which Ids are not in the array.

  • It is I saw, like $mnu is an array, the $menu is not, so it has no indexing, now, if you run the way it is there, it will check if it exists, then check if it is between 1 and 6, and within the if of between 1 and 6 you do the separation...

Show 4 more comments

Browser other questions tagged

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