How to check for certain values in an array?

Asked

Viewed 180 times

1

I have the variable $search_dorms who returns me Array ( [0] => 1 [1] => 2 ).

She can return too Array ( [0] => 1 [1] => 2 [2] => 3 )

Or simply Array ( [0] => 1 )

I am trying to check the returned values but without success. I ask for help. The code I am using is:

if(in_array(array('1'), $search_dorms)) {

    $search_dorms = 1;
    echo $search_dorms . "<br><br>";    

} elseif (in_array(array('1', '2'), $search_dorms)){

    $search_dorms = 2;
    echo $search_dorms . "<br><br>";        

}

How could I check if the values that can be strings or numbers are inside the array $search_dorms? If you notice, I’m trying to check more than 1 value simultaneously and that’s what’s catching.

Another way to explain what I said above:

if(in_array(1, $search_dorms)) echo "Ok<br><br>";
if(in_array(array(1, 2), $search_dorms)) echo "Ok novamente<br><br>";

I’m trying to do this check. The "OK again" does not appear.

  • 1

    If you had used the radiobutton I recommended, you wouldn’t have this problem. It would only be to see if it is 1, 2 or 3+ ...

  • @Bacco that cool who remembered and realized that it was for the same project ... is attentive ... rsrsrs ... radio button can not because it can have multiple choice.

  • 1

    The guy want 1 bedroom or 3+ and do not want 2? And it is easy to see that it is the same project, just array vc has a last 5 then kkkk. I think you have to rethink the logic of use. In fact if the guy chooses 2 dormitories, it is pq 1 only does not serve.

  • rsrsrsrs ... I am learning and very, very much with you ... taking scolds and ear tugs here, I ended up doing this whole project in PHPOO just with your tips.

  • @Bacco is a bit too stupid to think that the guy who scores 1 dorm will score 3 or vice versa more the customer wants so .. patience ...

  • Well, if so, the gambiara is the least of :) - I want to see how you will do the query of the search later. It will be "rolled up".

Show 1 more comment

1 answer

5

I took a look at the in_array documentation on http://php.net/manual/en/function.in-array.php and from what I understand the "ok again" is not appearing for the following reason:

in_array(array(1, 2), $search_dorms);

is looking for an array that contains the two integers, 1 and 2, within the $search_dorms array and not an integer 1 and integer 2 search in $search_dorms. If you do so:

var_dump(in_array(array(1, 2), $search_dorms));

you will see that it returns bool(false) (The reason for not entering the if block), but if you do:

$search_dorms = array(1, 2, array(1,2));
$foo = array(1,2);
var_dump($foo, $search_dorms);

will return bool(true).

it is also worth remembering that the third parameter of in_array() check the types. thus:

$search_dorms = array(1, 2, 3);
$foo = '1';
var_dump(in_array($foo, $search_dorms, true));

return bool(false)

to solve your problem I suggest you put the && operator inside the if.

if(in_array(1, $search_dorms) && in_array(2, $search_dorms)){...}

sources: in_array(): http://php.net/manual/en/function.in-array.php var_dump(): http://php.net/manual/en/function.var-dump.php

  • Dude you went deep to get this solution huh ... it will probably work for me ... I will do the tests ... congratulations !!!

Browser other questions tagged

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