I have a simple array like the following:
我有一个像下面这样的简单数组:
array 0 => string '101' 1 => string '105' 2 => string '103'
Desired result:
array(101, 105, 103)
Is this possible?
这可能吗?
1 个解决方案
#1
11
Yes, use array_values
.
是的,使用array_values。
array_values(array('0' => '101', '1' => '105', '2' => '103')); // returns array(101, 105, 103)
Edit: (Thanks to @MarkBaker)
编辑:(感谢@MarkBaker)
If you use var_dump
on the original array and the "values only" array the output might look exactly the same if the keys are numerical and and ascending beginning from 0. Just like in your example.
如果在原始数组和“仅值”数组上使用var_dump,则如果键是数字并且从0开始递增,则输出可能看起来完全相同。就像在示例中一样。
If the keys are not consisting of numbers or if the numbers are "random" then the output would be different. For example if the array looks like
如果键不是由数字组成或者数字是“随机”,则输出将是不同的。例如,如果数组看起来像
array('one' => '101', 'two' => '105', 'three' => '103')
the output of var_dump
looks different after converting the array with array_values
.
使用array_values转换数组后,var_dump的输出看起来不同。
#1
11
Yes, use array_values
.
是的,使用array_values。
array_values(array('0' => '101', '1' => '105', '2' => '103')); // returns array(101, 105, 103)
Edit: (Thanks to @MarkBaker)
编辑:(感谢@MarkBaker)
If you use var_dump
on the original array and the "values only" array the output might look exactly the same if the keys are numerical and and ascending beginning from 0. Just like in your example.
如果在原始数组和“仅值”数组上使用var_dump,则如果键是数字并且从0开始递增,则输出可能看起来完全相同。就像在示例中一样。
If the keys are not consisting of numbers or if the numbers are "random" then the output would be different. For example if the array looks like
如果键不是由数字组成或者数字是“随机”,则输出将是不同的。例如,如果数组看起来像
array('one' => '101', 'two' => '105', 'three' => '103')
the output of var_dump
looks different after converting the array with array_values
.
使用array_values转换数组后,var_dump的输出看起来不同。