-5

データに応じて、多次元配列からサブn個の配列を構築したいと思います。例:私はメイン配列を持っています

    Array
    (
    [0] => Array
        (
            [id] => 1
            [status] => -1
        )

    [1] => Array
        (
            [id] => 2
            [status] => 1
        )

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

    [3] => Array
        (
            [id] => 4
            [status] => 2
        )
    [4] => Array
        (
            [id] => 5
            [status] => 2
        )
)

この配列から3つの配列を取得したいのですが、次のようなステータス値のnoとタイプに依存します

  array{
    [0]=array(
        [status]=-1
        [count]=1

    )
    [1]=array(
        [status]=1
        [count]=1

    )
    [2]=array(
        [status]=2
        [count]=3

    )

}

前もって感謝します、

スニル・クマール P

4

1 に答える 1

0

このような意味ですか?:

$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
        )

)*/
于 2012-09-03T11:27:07.377 に答える