Check if a certain value exists within an array

Asked

Viewed 1,313 times

0

Colleagues,

we have a menu with 11 items and each item will open a page containing their respective PDF files. To avoid creating 11 different pages, we just create a page called pdf.php and put it in the links in this menu as follows:

<a href='?pag=1'>Orçamentário</a>
<a href='?pag=2'>Dados</a>
<a href='?pag=3'>Contas</a>

Up to link number 11 and we are rescuing within the same page that contains the menu in this way:

<?php if(isset($_REQUEST['pag'])){ include("pdf.php?pag=".$_REQUEST['pag']);} ?>

However, this will generate a querystring in the browser. Ex.: www.site.com.br? pag=1. So to prevent the user from typing the numbering directly into the wrong browser. Ex.: www.site.com.br? pag=12, we would like a message to appear to him Select an item from the menu.

To avoid giving an if() or switch() 11 times, we thought of creating an array with fixed values from 0 to 11 and if these values did not exist, the message would appear.

How could we do that? We thought about using in_array, but we couldn’t. See:

$array = array("0","1","2","3");

if(in_array(array(), $array)){
    echo "ok";
}else{
    echo "ops";
}
  • 1

    I think the question is dup

  • 1

    Besides being asked by Voce is passing the first wrong parameter the first is the value that Voce wants to find, not an empty array as used.

1 answer

2


Despite being a duplicated issue by the requested content, the error is specific to the way you are mounting your if, which should be as follows:

$array = array("0","1","2","3");

if(in_array($_GET['pag'], $array)){
    echo "ok";
}else{
    echo "ops";
}
  • 1

    Kenny, it would be interesting before answering a question, check if it is no longer duplicate :)

  • 1

    I believe I was very clear in my answer regarding this...Duplicate questions may have different solutions...

  • 2

    And how does it differ? http://answall.com/a/100965/14262

  • 1

    It’s not duplicated because the link you passed is different from my question. @Kennyrafael’s answer worked within my need.

  • 1

    @Marcelobonifazio, it differs that in this case, he already used the correct solution but in the wrong way, different from his example in which it was presented the person who asked...

  • on the links you gave me, I would have to use conditionals and that’s not my purpose.

Show 1 more comment

Browser other questions tagged

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