What does this chunk of PHP code do? My validations fail because of it

Asked

Viewed 97 times

-1

I’m working on a project that I’ve already picked up in progress and I’m having trouble displaying data for certain validations. I need to understand what this line does:

foreach ($queries as $id => $query) 
    if (!in_array($id, array('pagina', 'codigo', 'cidade', 'finalidade', 'tipo', 'imovel', 'bairro', 'dormitorios', 'valorMin', 'valorMax')) || empty($query)) 
        unset($queries[$id]);

Can anyone "translate"?

  • 2

    You are asking a lot of questions in a short time, so there is no problem, however your questions are getting badly formatted, please waste a little more time writing your questions, so we don’t have to fix them. Abs.

  • 1

    Another remark regarding your questions is that they could be answered with a simple query to the PHP documentation. Any and all PHP functions are very well documented, so you should have no trouble understanding simpler code snippets :)

1 answer

4

Line by line:

foreach ($queries as $id => $query) 

Loop over the array $queries. For each item, the value of the key will be stored in $id, and what is under the key will be stored in $query.

    if (!in_array($id, array('pagina', 'codigo', 'cidade', 'finalidade', 'tipo', 'imovel', 'bairro', 'dormitorios', 'valorMin', 'valorMax')) || empty($query)) 

If the current key is not one of the listed ones, or if the content of $query is empty

        unset($queries[$id]);

Removes the key $id array $queries.

Browser other questions tagged

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