I have a variable $v that can be either single string or array of strings
and I have a code:
$a = array(); if (is_array($v)) { $a = $v; } else { $a[] = $v; }
How it can be done in more elegant way? (in other words, how to cast a variable to array)
Any variable may be used as an array. There is no maximum limit to the size of an array, nor any requirement that member variables be indexed or assigned contiguously. Arrays are zero-based: the first element is indexed with the number 0.
You can cast a variable to an array by using:
$var = (array)$arr;
$a = (array) $v;
is the answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With