-4
  
[0] => Array
        (
            [id] => 9
            [order_address_id] => 9
            [created] => 2015-12-17 12:24:20
            [order_status] => dispatched
            [total_discount] => 
            [total_shipping_charge] => 60
            [s_state] => NY
            [b_state] => NY
            [order_id] => 9
            [p_name] => Product 1
            [qty] => 1
            [price] => 475
        )

    [1] => Array
        (
            [id] => 9
            [order_address_id] => 9
            [created] => 2015-12-17 12:24:20
            [order_status] => dispatched
            [total_discount] => 
            [total_shipping_charge] => 60
            [s_state] => MA
            [b_state] => MA
            [order_id] => 9
            [p_name] => Product 2
            [qty] => 1
            [price] => 349
        )

I want to find if array with same ID repeats then I have to delete total_shipping_charge from second and subsequent array having same id. How can I do it in PHP?

Mayur
  • 1
  • 2

1 Answers1

0

I would use array_walk for this.

First, you want to create a callback function that checks if the id has already been encountered, and unsets total_shipping_charge if it has, something like

function shipping_charge_walk(&$entry, $key, &$found_ids)
{
  if (in_array($entry['id'], $found_ids)) {
    unset($entry['total_shipping_charge']);
  } else {
    $found_ids[] = $entry['id'];
  }
}

This takes $found_ids by reference, so that it can append any encountered ids to it, and takes $entry by reference so that it can unset a member of it.

Then, simply call array walk on the array.

$found_ids = array();
array_walk($arr, 'shipping_charge_walk', $found_ids);
Brandon Horsley
  • 7,956
  • 1
  • 29
  • 28