PHP:比較數組的數組並在值匹配時建立新數組
P粉323224129
P粉323224129 2023-09-19 19:48:02
0
1
560

這裡是一個範例資料集

$all_items = array (
  $a1 = array (
      name => "item1",
      description => "item 1",
      item_key => 123
  ),
  $a2 = array (
      name => "item2",
      description => "item 2",
      item_key => 456
  ),
  $a3 = array (
      name => "item3",
      description => "item 3",
      item_key => 789
  )
);

$invoice_items = array (
  $b1 = array (
      item_key => 123,
      desc => "1item"
  ),
  $b2 = array (
      item_key => 456,
      desc => "2item"
  ),
  $b3 = array (
      item_key => 123,
      desc => "3item"
  ),
  $b4 = array (
      item_key => 345,
      desc => "4item"
  ),
  $b5 = array (
      item_key => 123,
      desc => "5item"
  ),
  $b6 = array (
      item_key => 345,
      desc => "6item"
  ),
  $b7 = array (
      item_key => 123,
      desc => "7item"
  )
);

所以這裡的目標是,無論何時有一個發票項目與一個項目的item_key匹配,我都希望將發票項目數組放入一個新數組中。所以在這個例子中,我認為我想要的結果是這樣的

Array (
   [0] => Array (
      [0] => Array ( [name] => item1 [description] => item 1 [item_key] => 123 ),
      [1] => Array ( [item_key] => 123 [desc] => 1item ),
      [2] => Array ( [item_key] => 123 [desc] => 3item ),
      [3] => Array ( [item_key] => 123 [desc] => 5item ),
      [4] => Array ( [item_key] => 123 [desc] => 7item )
   )

   [1] => Array (
      [0] => Array ( [name] => item2 [description] => item 2 [item_key] => 456 ),
      [1] => Array ( [item_key] => 456 [desc] => 2item ),
   )

   [2] => Array (
      [0] => Array ( [name] => item3 [description] => item 3 [item_key] => 789 )
   )
)

有什麼建議嗎?

我試過只比較數組並推送值,但最後只得到一個大數組,有點回到了起點。我對php還有些陌生,可能對一些陣列方法不熟悉

P粉323224129
P粉323224129

全部回覆(1)
P粉211273535
$all_items = [
  $a1 = [ 'name' => 'item1', 'description' => 'item 1', 'item_key' => 123 ],
  $a2 = [ 'name' => 'item2', 'description' => 'item 2', 'item_key' => 456 ],
  $a3 = [ 'name' => 'item3', 'description' => 'item 3', 'item_key' => 789 ]
];

$invoice_items = [
  $b1 = [ 'item_key' => 123, 'desc' => '1item' ],
  $b2 = [ 'item_key' => 456, 'desc' => '2item' ],
  $b3 = [ 'item_key' => 123, 'desc' => '3item' ],
  $b4 = [ 'item_key' => 345, 'desc' => '4item' ],
  $b5 = [ 'item_key' => 123, 'desc' => '5item' ],
  $b6 = [ 'item_key' => 345, 'desc' => '6item' ],
  $b7 = [ 'item_key' => 123, 'desc' => '7item' ]
];

$invoices = [];
foreach ($invoice_items as $invoice_item) {
  $invoices[$invoice_item['item_key']][] = $invoice_item;
}

$result = [];
foreach ($all_items as $all_item) {
  $result[] = array_merge([ $all_item ], $invoices[$all_item['item_key']] ?? []);
}

結果數組的結構與您在上面的問題中描述的相同。

但是您的結果集與您的註釋“...將invoice_item數字組合並到新數組中”之間存在矛盾。您的結果數組附加了發票項目,因此我上面的代碼。

現在,這個解決方案確實會創建一個新的鍵 - 這裡稱為“descs” - 在這個鍵下的發票項目中的一個數組:

$invoices = [];
foreach ($invoice_items as $invoice_item) {
  $invoices[$invoice_item['item_key']][] = $invoice_item;
}

foreach ($all_items as &$all_item) {
  $all_item['descs'] = $invoices[$all_item['item_key']] ?? [];
}
unset($all_item);

由於這些「descs」陣列條目中的item_key是多餘的,您可以將它們刪除並只建立一個字串陣列:

$invoices = [];
foreach ($invoice_items as $invoice_item) {
  $invoices[$invoice_item['item_key']][] = $invoice_item['desc'];
}

foreach ($all_items as &$all_item) {
  $all_item['descs'] = $invoices[$all_item['item_key']] ?? [];
}
unset($all_item);

輸出:

Array
(
    [0] => Array
        (
            [name] => item1
            [description] => item 1
            [item_key] => 123
            [descs] => Array
                (
                    [0] => 1item
                    [1] => 3item
                    [2] => 5item
                    [3] => 7item
                )

        )

    [1] => Array
        (
            [name] => item2
            [description] => item 2
            [item_key] => 456
            [descs] => Array
                (
                    [0] => 2item
                )

        )

    [2] => Array
        (
            [name] => item3
            [description] => item 3
            [item_key] => 789
            [descs] => Array
                (
                )

        )

)

請注意,您的輸入數組都在其構造中有賦值語句($a1 = ...,$a2 = ...等)。這沒有太多意義,除非您以後需要這些變數。如果是這樣,編碼方式更可讀性更好:

$a1 = [ 'name' => 'item1', 'description' => 'item 1', 'item_key' => 123 ],
$a2 = [ 'name' => 'item2', 'description' => 'item 2', 'item_key' => 456 ],
$a3 = [ 'name' => 'item3', 'description' => 'item 3', 'item_key' => 789 ]

$all_items = [ $a1, $a2, $a3 ];
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板