搜索

首页  >  问答  >  正文

PHP:比较数组的数组并在值匹配时创建新数组

这里是一个示例数据集

$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粉323224129P粉323224129438 天前567

全部回复(1)我来回复

  • P粉211273535

    P粉2112735352023-09-20 12:51:34

    $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 ];

    回复
    0
  • 取消回复