如何将变量转换为数组 [英] How to cast variable to array

查看:259
本文介绍了如何将变量转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量$ v,它可以是单个字符串或字符串数​​组
,并且我有一个代码:

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)

推荐答案

您可以使用以下方法将变量强制转换为数组:

You can cast a variable to an array by using:

    $var = (array)$arr;

这篇关于如何将变量转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆