In case your keys array do not repeat themselves between them you can do the following:
$todosArrays = $IdUserOnline2 + $IdUserComum2 + $IdUserNovo2;
$IdsInteracoes = implode(',',$todosArrays);
You can also use the function array_merge
if the keys to the array repeat
$todosArrays = array_merge($IdUserOnline2, $IdUserComum2, $IdUserNovo2);
$IdsInteracoes = implode(',',$todosArrays);
Recalling that in the case of array_merge
, check if the previous variables are arrays, avoiding mistakes:
$IdUserOnline2 = isset($IdUserOnline2) ? $IdUserOnline2 : array();
$IdUserComum2 = isset($IdUserComum2) ? $IdUserOnline2 : array();
$IdUserNovo2 = isset($IdUserNovo2) ? $IdUserOnline2 : array();
// É possível usar um ternário reduzido, porém é preciso inicializar as variáveis antes
// $IdUserOnline2 = $IdUserOnline2 ?: array();
// $IdUserComum2 = $IdUserComum2 ?: array();
// $IdUserNovo2 = $IdUserNovo2 ?: array();
$todosArrays = array_merge($IdUserOnline2, $IdUserComum2, $IdUserNovo2);
$IdsInteracoes = implode(',',$todosArrays);
The verification of conditions uses a ternary operator.
The logic is simple: Checks if the variable exists, if it does not exist, initializes it as a array blank. Full example.
PHP 7+
A new operator has been implemented in PHP 7 called Null Coalesce Operator, indicated for situations where it is necessary to set a default value if the previous expression returns null.
With it, even if the variable is not defined, PHP nay will generate a Notice
, functioning in the same way as ||
javascript.
So we could simplify the above code for:
$IdUserOnline2 = $IdUserOnline2 ?? [];
$IdUserComum2 = $IdUserComum2 ?? [];
$IdUserNovo2 = $IdUserNovo2 ?? [];
$todosArrays = array_merge($IdUserOnline2, $IdUserComum2, $IdUserNovo2);
$IdsInteracoes = implode(',',$todosArrays);
Our dear, could you explain what you wanted to do when you did this? develop a new logic for sure will be better than trying to fix this(believe) would be great :D you can edit your question with original intent?
– Paulo Roberto Rosa
The intention is already explicit in the question itself, I have to add several arrays to 1 only, however, at a given moment I need to add a prefix containing a "," to separate the arrays and not bugle, after joining the 3 I will have a string with all of them and pass it to a bind_param of mysqli.
– RodrigoBorth
Humm, why not add all values in a single Array and then create a repeat loop to add a
,
in each value? as,
are because of sql right?– Paulo Roberto Rosa
i could do this with merge array, but if one of them comes back empty of the error in the function, generating a string of errors, the check has to be individual and the action too...
– RodrigoBorth