[Solved] How to construct sub arrays from a main array


You mean like this?:

$array = array(
    array('id' => 1, 'status' => -1),
    array('id' => 2, 'status' => 1),
    array('id' => 3, 'status' => 2),
    array('id' => 4, 'status' => 2),
    array('id' => 5, 'status' => 2)
);


$statuses = array();
foreach($array as $one){
    if(!isset($statuses[$one['status']])){ $statuses[$one['status']] = 0; }
    $statuses[$one['status']]++;
}

$newArray = array();
foreach($statuses as $key => $val){
    $newArray[] = array('status' => $key, 'count' => $val);
}

print_r($newArray);


/*Array
(
    [0] => Array
        (
            [status] => -1
            [count] => 1
        )

    [1] => Array
        (
            [status] => 1
            [count] => 1
        )

    [2] => Array
        (
            [status] => 2
            [count] => 3
        )

)*/

2

solved How to construct sub arrays from a main array