hey I’m using PHP and I have this array called $memdeps
JavaScript
x
[0] => Array
(
[rel_id] => 2
[dep_id] => 4
[member_id] => 8
)
[1] => Array
(
[rel_id] => 3
[dep_id] => 1
[member_id] => 8
)
[2] => Array
(
[rel_id] => 4
[dep_id] => 3
[member_id] => 8
)
[3] => Array
(
[rel_id] => 9
[dep_id] => 3
[member_id] => 9
)
[4] => Array
(
[rel_id] => 8
[dep_id] => 1
[member_id] => 9
)
)
and I want to convert it to a form where the dep_id key contains all the related member_id’s
the wanted result is like that form
JavaScript
[dep_id] => Array
(
[0] => member_id 1
[1] => member_id 2
[2] => member_id 3
)
and the actually wanted result is like that
JavaScript
[0] => Array
(
[3] => Array
(
[0] => 8
[1] => 9
)
[4] => Array
(
[0] => 8
)
[1] => Array
(
[0] => 8
[1] => 9
)
)
I tried that code
JavaScript
$memdep = array();
foreach($memdeps as $key => $value){
$memdep[] = $memdep[][$value->dep_id] = $value->member_id;
}
and it’s not working , and I don’t know how to be able to do it
Advertisement
Answer
Here is the solution
JavaScript
$memdep = array();
foreach($memdeps as $key => $value)
{
$memdep[$value->dep_id][] = $value->member_id;
}