I think you just mess up with proper array notation here. Array notation no uses curly braces {
or }
. So If it is a proper array then you can try like this way using array_chunk()
N.B : For array notation we can use two different ways e.g $array = []
or $array=array()
<?php
$array = [
'category.index',
'category.create',
'category.store',
'category.edit',
'product.index',
'product.create',
'product.store',
'product.edit',
'city.index',
'city.create',
'city.store',
'city.edit',
];
print '<pre>';
print_r(array_chunk($array,4));
print '</pre>';
?>
Output:
Array
(
[0] => Array
(
[0] => category.index
[1] => category.create
[2] => category.store
[3] => category.edit
)
[1] => Array
(
[0] => product.index
[1] => product.create
[2] => product.store
[3] => product.edit
)
[2] => Array
(
[0] => city.index
[1] => city.create
[2] => city.store
[3] => city.edit
)
)
DEMO: https://eval.in/980164
EDIT:
AS PER YOUR QUESTION UPDATE
Simply try like this with list()
$new = array();
foreach($array as $val) {
list($item, $number) = explode('.', $val);
$new[$item][] = $val;
}
print '<pre>';
print_r(array_values($new));
print '</pre>';
DEMO: https://eval.in/980176
Output:
Array
(
[0] => Array
(
[0] => category.index
[1] => category.create
[2] => category.store
[3] => category.edit
)
[1] => Array
(
[0] => product.index
[1] => product.create
[2] => product.store
)
[2] => Array
(
[0] => city.index
[1] => city.create
[2] => city.store
[3] => city.edit
)
)
EDITED AGAIN
As you’ve different initial array on your question now so now code should be like this.
$new = array();
foreach($array as $val) {
list($item, $number) = explode('-', $val);
$new[$number][] = $val;
}
print '<pre>';
print_r(array_values($new));
print '</pre>';
DEMO: https://eval.in/980251
4
solved How to separating arrays in php? [closed]