php 如何将变量转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5970270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to cast variable to array
提问by tsds
I have a variable $v that can be either single string or array of strings
and I have a code:
我有一个变量 $v 可以是单个字符串或字符串数组,
我有一个代码:
$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)
如何以更优雅的方式完成它?(换句话说,如何将变量转换为数组)
回答by cbroughton
You can cast a variable to an array by using:
您可以使用以下方法将变量转换为数组:
$var = (array)$arr;
回答by Kelly
$a = (array) $v;
is the answer.
是答案。
回答by kapa
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);
回答by mario
回答by TroySteven
If you are looking to convert an object to a single count array you can use the follow code:
如果您希望将对象转换为单个计数数组,您可以使用以下代码:
$list = array([0] => $obj);
The other provided answers won't work when trying to convert an object, it will simply convert the fields of that object into an associative array (unless that is what you are trying to do).
其他提供的答案在尝试转换对象时不起作用,它只会将该对象的字段转换为关联数组(除非您正在尝试这样做)。
$var = (array)$arr;
回答by GuyPaddock
As others have said, casting a scalarvalue to an array will produce a singleton array (i.e. an array with the scalar as its only element). However, as still others have pointed out, take care to only do this if you know the value is going to be a scalar and not a class instance.
正如其他人所说,将标量值转换为数组将产生一个单例数组(即以标量作为唯一元素的数组)。但是,正如其他人指出的那样,只有在您知道值将是标量而不是类实例时才注意这样做。
From the PHP docs:
来自PHP 文档:
For any of the types
integer
,float
,string
,boolean
andresource
, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words,(array)$scalarValue
is exactly the same asarray($scalarValue)
.If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.
对于任何类型的
integer
,float
,string
,boolean
和resource
,在数组中的值转换到一个数组结果与索引零的单个元件和将其转化标量的值。换句话说,(array)$scalarValue
与 完全相同array($scalarValue)
。如果将对象转换为数组,则结果是一个数组,其元素是对象的属性。键是成员变量名,有几个值得注意的例外:整数属性不可访问;私有变量在变量名前加上了类名;受保护的变量在变量名前有一个“*”。这些前置值在任一侧都有空字节。
回答by opex
Actually if you want to cast to an array and not have to worry about what you put into it, the answer is
实际上,如果您想转换为数组而不必担心放入其中的内容,答案是
$var = (is_object($var)) ? array($var) : (array) $var;
You can test this with the following code
您可以使用以下代码进行测试
function toArray($var) {
return (is_object($var)) ? array($var) : (array) $var;
}
$object = new stdClass;
$resource = fopen('php://stdout', 'w');
$closure = function () {};
$tests = array(
array(toArray(true), array(true), 'boolean true'),
array(toArray(false), array(false), 'boolean false'),
array(toArray(null), array(), 'null'),
array(toArray(1), array(1), 'positive integer'),
array(toArray(0), array(0), 'zero integer'),
array(toArray(-1), array(-1), 'negative integer'),
array(toArray(1.5), array(1.5), 'positive float'),
array(toArray(0.0), array(0.0), 'zero float'),
array(toArray(-1.5), array(-1.5), 'negative float'),
array(toArray(''), array(''), 'empty string'),
array(toArray('foo'), array('foo'), 'string'),
array(toArray(array()), array(), 'array'),
array(toArray($object), array($object), 'object'),
array(toArray($resource), array($resource), 'resource'),
array(toArray($closure), array($closure), 'closure'),
);
foreach ($tests as $test) {
ob_start();
var_dump($test[0]);
$a = ob_get_clean();
ob_start();
var_dump($test[1]);
$b = ob_get_clean();
assert($a === $b, "{$test[2]} is not the same");
}