Home  >  Q&A  >  body text

PHP: Compare arrays of arrays and create new array when values ​​match

Here is a sample data set

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

So the goal here is that whenever there is an invoice item that matches an item's item_key, I want to put the array of invoice items into a new array. So in this example, I think the result I want is something like this

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

Any suggestions?

I've tried just comparing the arrays and pushing the values, but I just end up with a big array, kind of back to where I started. I'm still a little new to PHP and may not be familiar with some array methods

P粉323224129P粉323224129419 days ago545

reply all(1)I'll reply

  • 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']] ?? []);
    }

    The structure of the resulting array is the same as you described in your question above.

    But there is a contradiction between your result set and your comment "...merge the invoice_item array into a new array". Your result array has the invoice items appended, hence my code above.

    Now, this solution does create a new key - here called "descs" - an array in the invoice items under this key:

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

    Since the item_keys in these "descs" array entries are redundant, you can remove them and just create a string array:

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

    Output:

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

    Please note that your input arrays all have assignment statements in their construction ($a1 = ..., $a2 = ..., etc.). This doesn't make much sense unless you need these variables later. If so, encoding is more readable and better:

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

    reply
    0
  • Cancelreply