使用爆炸在数组中拆分值以形成多维数组 [英] 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] =>数组(3)
[0] => 0
[1] => 245.19000000
[2] => 864432
[2] =>数组(3)
[0] => 1
[1] => 245.26000000
[2] => 864432
[3] =>数组(3)
[0] => 3
[1] => 245.49000000
[2] => 864432
[4] =>数组(3)
[0] => 4
[1] => 245.3000000
[2] => 864432
[4] =>数组(3)
[0] => 5
[1] => 245.3300000
[2] => 864432
}
}
...
我到目前为止:
$ done = array();
for($ i = 0; $ i< = count($ chunks); $ i ++){//循环获取每个2d数组的大小
$ r = count($ chunks [$ i]);
for($ c = 0; $ c< = count($ chunks [$ r]); $ c ++){//循环遍历3d数组
$ arrayparts = $ chunks [$ i] [$ c];
$ done [] = explode(,,$ arrayparts); // $ arrayparts是3d数组字符串,每次通过循环
}
}
我认为这段代码应该可以工作,但是当我var_dump时什么也没打印?
有人可以帮助我学习吗?
谢谢!
建议:
$ chunks是3d数组
foreach($ chunks as $ innerArray){
$ result [] = array_map(function($ v){
return explode(,,$ v);
},$ innerArray);
}
不要太复杂,只需使用以下命令即可:
(这里我使用foreach循环遍历每个innerArray,然后使用 array_map()
并将其分解并返回结果数组)
<?php
foreach($ arr as $ innerArray){
$ result [] = array_map(function($ v){
return explode(,,$ v);
},$ innerArray);
}
print_r($ result);
?>
I am new to PHP so be kind. :) I have a 3 deep array. Something like this:
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"
}
}...
I want to explode the inner string by commas ("2,245.49000000,864453") so the arrays becomes 4 deep like so:
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"
}
}
...
So far I have:
$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
}
}
I think this code should work but when I var_dump nothing prints? Can someone help me learn?
Thanks!
Suggested: $chunks is 3d array
foreach($chunks as $innerArray) {
$result[] = array_map(function($v){
return explode(",", $v);
}, $innerArray);
}
Don't make it complicated, just use this:
(Here I go through each innerArray with a foreach loop and then I go through all values with array_map()
and explode it and return it to the results array)
<?php
foreach($arr as $innerArray) {
$result[] = array_map(function($v){
return explode(",", $v);
}, $innerArray);
}
print_r($result);
?>
这篇关于使用爆炸在数组中拆分值以形成多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!