How to cast variable to array

前端 未结 8 2077
抹茶落季
抹茶落季 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);
    

提交回复
热议问题