How to insert an instruction into the array only if it is true?

Asked

Viewed 98 times

-2

I need to somehow verify if there is any element within a given array and if there is, put a whole string in a specific place. For example:

I need to check whether $categoria is not empty

$categoria = array($values);

And after checking that it is not empty, insert the string below inside filter

$categoria = " 'Categoria' => array($values) ";

The array is this and I have to insert these instructions into the filter:

$dados = array (
    'fields' => array(
        'ImoCodigo', 
        'Categoria', 
        'Bairro',
        'ValorVenda', 
        'Dormitorios', 
        'Vagas', 
        'FotoDestaque'
    ),
    'filter' => array(
        // As strings devem ser inseridas exatamente aqui separadas por vírgula
    ),
    'paginacao' => array(
        'pagina' => 1,
        'quantidade' => 50
    )   
);

The end result would look like this:

...
'filter' => array(
    'Categoria' => array($values),
    'Cidade' => array($cidades),
    'ValorVenda' => array($preco)
),
...
  • $dados['filter'][] = $categoria, since the category is string. I do this to generate JSON and it works. try there. If it doesn’t work, use array_push(). Function Docs: http://php.net/manual/en/function.array-push.php

  • Caro @Eduardoalmeida is exactly what I’m using for :)

  • Dear @Eduardoalmeida can assemble a response showing the feat please :)

1 answer

2


Checks that the value is not empty. If empty, arrow is null.

Example of how to add to index filter array $dados:

'filter' => array(
    'categoria' => ((!empty($values))? array($values) : null)
),

But it seems strange to assign $values as an array array($values). Is that right? You’re just creating unnecessary memory space.

If you do not want the index categoria when empty, make the following logic:

Create the array $dados normally, however, without the "filter"

$dados = array (
 ....
);

With the array created, just add or remove the indexes as you wish:

((!empty($values))? $dados['filter']['categoria'] = array($values) : '');

Full example:

$dados = array (
    'fields' => array(
        'ImoCodigo', 
        'Categoria', 
        'Bairro',
        'ValorVenda', 
        'Dormitorios', 
        'Vagas', 
        'FotoDestaque'
    ),
    'paginacao' => array(
        'pagina' => 1,
        'quantidade' => 50
    )   
);
((!empty($values))? $dados['filter']['categoria'] = array($values) : '');
  • Caro @Daniel if the field is empty, categoria cannot appear there.

  • The original answer was enough. Just understand and implement for your case. But I edited with an example for your specific case.

  • 1

    don’t ask me how to add $city and $valueVenda, then I give you a negative.. rsrs.. just follow the same logic.

  • If you look at this page http://new.pier36imoveis.com.br/teste.php will repair this output: "Casa","Apartamento" ... can help me to have exactly this output of $values ... Thank you, I’m learning, thank you for your patience.

  • The instructions above. The rest, how to implement, is your responsibility.

  • Thank you for demonstrating that arrays can be imported inside other arrays ... This will help me a lot.

Show 1 more comment

Browser other questions tagged

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