Assign multiple array to a variable dynamically

0

Hello, I need to assign multiple array to a single variable, the value of all of them will be in sequence in that variable. Ex.:

$variavel = $array[0] . $array[1] . $array[2] . $array[3] . $array[4];
echo $variavel

This variable then uses it in an IF to make multiple comparisons, how can I do this without having to write infinite array assignments?

    
asked by anonymous 14.11.2018 / 14:35

2 answers

2

Use implode to join / concatenate array values

echo implode(array);

See the result: link

On if , if comparisons are made, it is better to treat the array directly.

You can use the following functions:

  • in_array - search if a value exists in the array;
  • array_filter - filter the array and return the values that passed through the filter;
  • array_reduce - applies a function and reduces an array to a single value. The two previous functions are the most recommended, however, if you need to, you can apply a function and return true / false.

Comparing the string directly may not yield consistent results. I will not go into performance issues as there is no need.

    
14.11.2018 / 14:38
-1

change:

echo $variavel;

To:

echo serealize($varialvel);

or

echo json_encode($variavel);
    
14.11.2018 / 18:03