How to cast variable to array

前端 未结 8 2008
抹茶落季
抹茶落季 2020-12-17 07:54

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 {
    $         


        
相关标签:
8条回答
  • 2020-12-17 08:24

    I would write your could snippet like this (short and you read it and know exactly what is happening):

    $a = is_array($v) ? $v : array($v);
    
    0 讨论(0)
  • 2020-12-17 08:25

    You can cast a variable to an array by using:

        $var = (array)$arr;
    
    0 讨论(0)
提交回复
热议问题