我要转换以下值
Array
(
[0]=>4,9
)
此值
Array
(
[0]=>4
[1]=>9
)
答案 0 :(得分:2)
您可以使用以下解决方案:
<?php
//the current array and an empty new array to get the result.
$cur_array = [0 => "4,9", 1 => "5,9"];
$new_array = [];
//run through all array values to convert these.
for ($i = 0; $i < count($cur_array); $i++) {
//get an array of the comma separated list.
$part = explode(',', $cur_array[$i]);
//add the array of comma separated list to the new array (result).
$new_array = array_merge($new_array, $part);
}
//convert all string values to integer values.
for ($j = 0; $j < count($new_array); $j++) {
$new_array[$j] = intval($new_array[$j]);
}
//output of the new array (debug).
var_dump($new_array);
...或以下使用array_values
的更紧凑的解决方案:
<?php
$oldarr = [0 => "4,9", 1 => "5,9"];
$newarr = [];
foreach (array_values($oldarr) as $arrpart) {
$newarr = array_merge($newarr, explode(',', $arrpart));
}
var_dump($newarr);
如果您的值是十进制数字,则可以使用以下内容:
<?php
$oldarr = [0 => 4.9, 1 => 5.9, 2 => 6];
$newarr = [];
foreach (array_values($oldarr) as $arrpart) {
$newarr = array_merge($newarr, explode('.', $arrpart));
}
var_dump($newarr);
答案 1 :(得分:0)
$array = array( 0 => '4,9');
foreach($array as $data){
$commaSeparatedArr = explode(',',$data);
}
print_r($commaSeparatedArr);
答案 2 :(得分:0)
使用爆破和爆炸。
首先,使用逗号将数组中的所有值内插,然后以逗号爆炸。
$result = explode(",", implode(",", $array));
答案 3 :(得分:-1)
$oldarr = [0 => 4,9];
$string = implode(',',$oldarr);
$new_array = explode(',',$string);
print_r($new_array);
答案 4 :(得分:-1)
替换数组的值。
$array = ["4,9,7,14", "7,9,68"];
$new_array = [];
foreach($array as $a) {
array_map(function($a_r) {
$new_array[] = (int) $a_r
}, exlpode(",", $a));
}
var_dump($new_array);
/* Output
Array (
[0] => int 4,
[1] => int 9,
[2] => int 7,
[3] => int 14,
[4] => int 7,
[5] => int 9,
[6] => int 68
)
*/