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.
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.
$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
Another way to do this without foreach loop. :->
$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);