簡體   English   中英

如何在php中對多維關聯數組的重復值進行分組?

[英]How can I group repeated values of a multidimensional associative array in php?

將數組的重復值分組的最佳方法是:

$array = [
        0 => ['id'=>1, 'name'=>'prod1', 'type'=>'a', 'price'=>2.50],
        1 => ['id'=>1, 'name'=>'prod1', 'type'=>'b', 'price'=>5.50],
        2 => ['id'=>1, 'name'=>'prod1', 'type'=>'c', 'price'=>10.50],
        3 => ['id'=>2, 'name'=>'prod2', 'type'=>'a', 'price'=>3.50],
        4 => ['id'=>2, 'name'=>'prod2', 'type'=>'b', 'price'=>7.50]
      ]; 

為了得到一個像這樣的輸出數組:

$sorted_array = [
    0 => ['id'=>1, 'name'=>'prod1', 'type'=>['a' => ['price'=>2.50], 
                                             'b'=> ['price'=>5.50], 
                                             'c'=> ['price'=>10.50]
                                            ]
         ],
    1 => ['id'=>2, 'name'=>'prod2', 'type'=>['a'=>['price'=>3.50],
                                             'b'=>['price'=>7.50]
                                            ]
          ]

  ]; 

這不是最好的方法 ,但它可以做你想要的:

$sorted_array = [];
for($i = 0; $i < count($array); $i++){
    $k = $array[$i]['id']-1;
    if($sorted_array[$k] == null){
        $sorted_array[$k]['id'] = $array[$i]['id'];
        $sorted_array[$k]['name'] = $array[$i]['name'];
    }
    $sorted_array[$k]['type'][$array[$i]['type']]['price'] = $array[$i]['price'];
}

我在使用你的陣列的本地服務器上試了一下,得到了這個:

Array
(
[0] => Array
    (
        [id] => 1
        [name] => prod1
        [type] => Array
            (
                [a] => Array
                    (
                        [price] => 2.5
                    )

                [b] => Array
                    (
                        [price] => 5.5
                    )

                [c] => Array
                    (
                        [price] => 10.5
                    )

            )

    )

[1] => Array
    (
        [id] => 2
        [name] => prod2
        [type] => Array
            (
                [a] => Array
                    (
                        [price] => 3.5
                    )

                [b] => Array
                    (
                        [price] => 7.5
                    )

            )

    )

)

Zeke,我把你的方法稍微改了一下,因為編譯器抱怨一些“Undefined offsets”。

這是現在的樣子:

$sorted_array = [];

for($i = 0; $i < count($array); $i++){
    if($i == 0){
        $sorted_array[$i]['id'] = $array[$i]['id'];
        $sorted_array[$i]['name'] = $array[$i]['name'];
        $sorted_array[$i]['type'][$array[$i]['type']]['price'] = $array[$i]['price'];
    }else{
         $last = end($sorted_array);
         $key = key($sorted_array);
         if($last['id']==$array[$i]['id']){
             $sorted_array[$key]['type'][$array[$i]['type']]['price'] = $array[$i]['price'];
         }else{
             $k = $key+1;
             $sorted_array[$k]['id'] = $array[$i]['id'];
             $sorted_array[$k]['name'] = $array[$i]['name'];
             $sorted_array[$k]['type'][$array[$i]['type']]['price'] = $array[$i]['price'];
         }
    }
}

輸出:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => prod1
            [type] => Array
                (
                    [a] => Array
                        (
                            [price] => 2.5
                        )

                    [b] => Array
                        (
                            [price] => 5.5
                        )

                    [c] => Array
                        (
                            [price] => 10.5
                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => prod2
            [type] => Array
                (
                    [a] => Array
                        (
                            [price] => 3.5
                        )

                    [b] => Array
                        (
                            [price] => 7.5
                        )

                )

        )

)

感謝一百萬Zeke,你的回答對我很有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM