I have an array like below.
我有一个如下所示的数组。
$arr=array('0_1','0_3','1_2',1_1','4_1');
can i divide it into
我可以把它分成
$arr[0][1]='0_1';
$arr[0][3]='0_3';
... Thanks.
... 谢谢。
3 个解决方案
#1
4
$newArray = [];
foreach ($arr as $item) {
$items = explode('_', $item);
$newArray[$items[0]][$items[1]] = $item;
}
var_dump($newArray);
exit();
This should do the trick. You should have a look at the explode function
这应该可以解决问题。你应该看看爆炸功能
#2
2
Another way to do this without foreach
loop. :->
另一种没有foreach循环的方法。 - >
$arr = array('0_1','0_3','1_2','1_1','4_1');
$result = [];
array_walk($arr,function($v,$k)use (&$result){
$data = explode("_",$v);
$result[$data[0]][$data[1]] = $v;
});
print_r($result);
#3
1
$arrv = array('0_1','0_3','1_2','1_1','4_1');
$newArray = [];
array_walk($arrv,function($value,$key){
global $newArray;
$indexArray = explode("_",$value);
$newArray[$indexArray[0]][$indexArray[1]] = $value;
});
var_dump($newArray);
#1
4
$newArray = [];
foreach ($arr as $item) {
$items = explode('_', $item);
$newArray[$items[0]][$items[1]] = $item;
}
var_dump($newArray);
exit();
This should do the trick. You should have a look at the explode function
这应该可以解决问题。你应该看看爆炸功能
#2
2
Another way to do this without foreach
loop. :->
另一种没有foreach循环的方法。 - >
$arr = array('0_1','0_3','1_2','1_1','4_1');
$result = [];
array_walk($arr,function($v,$k)use (&$result){
$data = explode("_",$v);
$result[$data[0]][$data[1]] = $v;
});
print_r($result);
#3
1
$arrv = array('0_1','0_3','1_2','1_1','4_1');
$newArray = [];
array_walk($arrv,function($value,$key){
global $newArray;
$indexArray = explode("_",$value);
$newArray[$indexArray[0]][$indexArray[1]] = $value;
});
var_dump($newArray);