使用 Explode 拆分数组中的值以形成多维数组
Split Values in Array Using Explode to form Multidimensional Array
我是 PHP 的新手,请多关照。 :)
我有一个 3 深阵列。像这样:
Array(5) {
[0]=> array(5) {
[0]=> string(0) ""
[1]=> string(21) "0,245.19000000,864432"
[2]=> string(21) "1,245.26000000,864432"
[3]=> string(21) "2,245.49000000,864432"
[4]=> string(21) "4,245.33000000,864432"
}
[1]=> array(5) {
[0]=> string(0) ""
[1]=> string(21) "0,245.19000000,864453"
[2]=> string(21) "1,245.26000000,864453"
[3]=> string(21) "2,245.49000000,864453"
[4]=> string(21) "4,245.33000000,864453"
}
}...
我想用逗号 ("2,245.49000000,864453") 展开内部字符串,这样数组就变成了 4 深,如下所示:
Array(5) {
[0]=> array(5) {
[0]=> string(0) ""
[1]=> array (3)
[0]=> "0"
[1]=> "245.19000000"
[2]=> "864432"
[2]=> array (3)
[0]=> "1"
[1]=> "245.26000000"
[2]=> "864432"
[3]=> array (3)
[0]=> "3"
[1]=> "245.49000000"
[2]=> "864432"
[4]=> array (3)
[0]=> "4"
[1]=> "245.3000000"
[2]=> "864432"
[4]=> array (3)
[0]=> "5"
[1]=> "245.3300000"
[2]=> "864432"
}
}
...
到目前为止我有:
$done = array();
for ($i = 0; $i<=count($chunks); $i++) { //loops to get size of each 2d array
$r = count($chunks[$i]);
for ($c = 0; $c<=count($chunks[$r]); $c++) { //loops through 3d array
$arrayparts = $chunks[$i][$c];
$done[] = explode(",", $arrayparts); //$arrayparts is 3d array string that is exploded each time through loop
}
}
我认为这段代码应该可以工作,但是当我 var_dump 没有打印任何内容时?
有人可以帮我学习吗?
谢谢!
建议:
$chunks 是 3d 数组
foreach($chunks as $innerArray) {
$result[] = array_map(function($v){
return explode(",", $v);
}, $innerArray);
}
不要搞复杂,直接用这个:
(在这里,我使用 foreach 循环遍历每个 innerArray,然后使用 array_map()
遍历所有值并将其分解,然后 return 将其分解为结果数组)
<?php
foreach($arr as $innerArray) {
$result[] = array_map(function($v){
return explode(",", $v);
}, $innerArray);
}
print_r($result);
?>
这使用了 array_map()
两次。也许是更好的方法,但我正在喝三杯啤酒:
$result = array_map(function($v){
return array_map(function($v){ return explode(',', $v); }, $v);
}, $array);
我是 PHP 的新手,请多关照。 :) 我有一个 3 深阵列。像这样:
Array(5) {
[0]=> array(5) {
[0]=> string(0) ""
[1]=> string(21) "0,245.19000000,864432"
[2]=> string(21) "1,245.26000000,864432"
[3]=> string(21) "2,245.49000000,864432"
[4]=> string(21) "4,245.33000000,864432"
}
[1]=> array(5) {
[0]=> string(0) ""
[1]=> string(21) "0,245.19000000,864453"
[2]=> string(21) "1,245.26000000,864453"
[3]=> string(21) "2,245.49000000,864453"
[4]=> string(21) "4,245.33000000,864453"
}
}...
我想用逗号 ("2,245.49000000,864453") 展开内部字符串,这样数组就变成了 4 深,如下所示:
Array(5) {
[0]=> array(5) {
[0]=> string(0) ""
[1]=> array (3)
[0]=> "0"
[1]=> "245.19000000"
[2]=> "864432"
[2]=> array (3)
[0]=> "1"
[1]=> "245.26000000"
[2]=> "864432"
[3]=> array (3)
[0]=> "3"
[1]=> "245.49000000"
[2]=> "864432"
[4]=> array (3)
[0]=> "4"
[1]=> "245.3000000"
[2]=> "864432"
[4]=> array (3)
[0]=> "5"
[1]=> "245.3300000"
[2]=> "864432"
}
}
...
到目前为止我有:
$done = array();
for ($i = 0; $i<=count($chunks); $i++) { //loops to get size of each 2d array
$r = count($chunks[$i]);
for ($c = 0; $c<=count($chunks[$r]); $c++) { //loops through 3d array
$arrayparts = $chunks[$i][$c];
$done[] = explode(",", $arrayparts); //$arrayparts is 3d array string that is exploded each time through loop
}
}
我认为这段代码应该可以工作,但是当我 var_dump 没有打印任何内容时? 有人可以帮我学习吗?
谢谢!
建议: $chunks 是 3d 数组
foreach($chunks as $innerArray) {
$result[] = array_map(function($v){
return explode(",", $v);
}, $innerArray);
}
不要搞复杂,直接用这个:
(在这里,我使用 foreach 循环遍历每个 innerArray,然后使用 array_map()
遍历所有值并将其分解,然后 return 将其分解为结果数组)
<?php
foreach($arr as $innerArray) {
$result[] = array_map(function($v){
return explode(",", $v);
}, $innerArray);
}
print_r($result);
?>
这使用了 array_map()
两次。也许是更好的方法,但我正在喝三杯啤酒:
$result = array_map(function($v){
return array_map(function($v){ return explode(',', $v); }, $v);
}, $array);