簡體   English   中英

PHP - 按鍵數組對數組進行分組

[英]PHP - Group array by array of keys

這幾天我一直在考慮這個問題,而 Stack Overflow 是我最后的選擇。

我想知道如何從左側結構到右側結構:

From this:                                        To this:
-----------------------------------               -----------------------------------
| R1   | NULL,T1,T2,T3,T4,T5,T6   |               | NULL        | T1,T2,T5          |
| R2   | T1,T2,T3                 |               | R1          | NULL              |
| R3   | T1,T2,T3                 |   -------->   | R1,R2,R3,R4 | T1,T2             |
| R4   | T1,T2                    |               | R1,R2,R3    | T3                |
| NULL | T1,T2,T5                 |               | R1          | T4,T5,T6          |
-----------------------------------               -----------------------------------

注釋/規則:

  • 如果 value 或 category 為NULL ,請為其創建一個單獨的組。
  • 首先,嘗試使用盡可能多的 R(道具:值)來創建組。 所以它導致 [R1,R2,R3,R4 | T1,T2] 而不是 [R1,R2,R3 | T1,T2,T3]
  • 我不是在尋找復制粘貼的答案,但我已經為此煩惱了好幾天,我希望得到一些如何在 PHP 中正確執行此操作的建議。

在 PHP 中

$input = [
  [
    'value' => null,
    'categories' => ['T1', 'T2', 'T5']
  ],
  [
    'value' => 'R1',
    'categories' => [NULL, 'T1', 'T2', 'T3', 'T4', 'T5', 'T6']
  ],
  [
    'value' => 'R2',
    'categories' => ['T1', 'T2', 'T3']
  ],
  [
    'value' => 'R3',
    'categories' => ['T1', 'T2', 'T3']
  ],
  [
    'value' => 'R4',
    'categories' => ['T1', 'T2']
  ]
]

// DESIRED OUTPUT
$input = [
  [
    'values' => [NULL],
    'categories' => ['T1', 'T2', 'T5']
  ],
  [
    'values' => ['R1'],
    'categories' => [NULL]
  ],
  [
    'values' => ['R1', 'R2', 'R3', 'R4'],
    'categories' => ['T1', 'T2']
  ],
  [
    'values' => ['R1', 'R2', 'R3'],
    'categories' => ['T3']
  ],
  [
    'value' => 'R1',
    'categories' => ['T4', 'T5', 'T6']
  ]
]

這里的邏輯不是按values (Rx)工作,而是按categories (Tx)工作。

如果您過濾輸入:

  • Rx或類別Tx是否為空?
  • 按類別Tx => Rx

您可以收集給定Tx的所有Rx 您最終(不包括空值)得到一個數組,其中值Rx按類別Tx排序。 例如:

T1 => [ R1, R2, R3, R4 ]
T2 => [ R1, R2, R3, R4 ]
T3 => [ R1, R2, R3 ]

現在您必須按Rx范圍對數組進行分組,以便將所有[R1, R2, R3, R4 ]連接到一個結果數組中:

[ R1, R2, R3, R4 ] => [ T1, T2 ]

這里的訣竅是使[ R1, R2, R3, R4 ]成為一個字符串,以便它可以用作鍵(數組鍵本身不能是數組)。 確保首先對數組進行排序 然后收集每個字符串的所有Tx ,同時保存實際的Rx數組:

R1R2R3R4 => [ values => [ R1, R2, R3, R4 ], categories => [ T1, T2 ] ]
R1R2R3   => [ values => [ R1, R2, R3 ], categories => [ T3 ] ]

將該數組與空值一起使用來創建結果。

//set some arrays
$nulls=[];
$categories=[];
$tmp=[];

//loop $INPUT
foreach($input as $inp){

    // VALUE (Rx) = NULL :: put in $nulls
    if($inp['value']===NULL){
        $nulls[]=['values'=>[NULL],'categories'=>$inp['categories']];
        continue;
        }
    // ELSE use VALUE (Rx)
    $Rx=$inp['value'];
    
    //loop CATEGORIES (Tx)
    foreach($inp['categories'] as $Tx){
        // CATEGORY (Tx) = NULL :: put in $nulls
        if($Tx===NULL){
            $nulls[]=['values'=>[$Rx],'categories'=>[null]];
            continue;
            }
        // push $categories [ Tx ] [] => VALUE (Rx) 
        $categories[$Tx][]=$Rx;
        }
    }

//sort $categories Tx => [ Rx, Rx, Rx ]
foreach($categories as $Tx=>$Rx){
    // create a KEY STRING R1R2R3R4.
    // Make sure to first SORT the values, so the order is consistent!
    sort($Rx);
    $string=implode('',$Rx);
    // if not exists $tmp [ KEY STRING ]
    // create $tmp [ KEY STRING ] => [ values=>[ Rx] , categories[ ] ]
    if(!isset($tmp[$string]))
        $tmp[$string]=[
            'values'=>$Rx,
            'categories'=>[]
            ];
    // push $tmp [  KEY STRING ][ categories ][] = Tx
    $tmp[$string]['categories'][]=$Tx;
    }

// create $result
// $nulls has either values or is empty array. So we can always use it.
$result=$nulls;
// loop TMP, adding [ values=>[ Rx] , categories[ Tx ] ]
foreach($tmp as $v){
    $result[]=[
            'values'=>$v['values'],
            'categories'=>$v['categories']
            ];
    }

暫無
暫無

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

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