1

I have an array like this:

    array (
      0 => 
      array (
        'title' => 'HELLO';
        'nid' => '50',              //////DUPLICATE
        'Order_id' => '240',
      ),
      1 => 
      array (
        'title' => 'My View';
        'nid' => '51',
        'Order_id' => '220',
      ),
      2 => 
      array (
        'title' => 'HELLO';
        'nid' => '50',              //////DUPLICATE
        'Order_id' => '200',
      ),
      3 => 
      array (
        'title' => 'Starts';
        'nid' => '54',
        'Order_id' => '195',
      ),
    )

I need to remove duplicate Arrays by using Max Order_id and sort by maximal Order_id In this example.

Any help on doing this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bijan Zand
  • 51
  • 8
  • Was there a typing mistake since Order ID of arrays marked duplicate are different i.e., 240 and 200. – ZerosAndOnes Nov 25 '17 at 10:13
  • And are you trying to remove the arrays having duplicate 'nid's or arrays that match completely? – ZerosAndOnes Nov 25 '17 at 10:19
  • Not mistae..2 field with same NID and same Title, every time user has new order ORDER_ID is UPDATED and added New ORDER_ID, so cause to dublicate fields....! – Bijan Zand Nov 25 '17 at 10:20
  • I want to remove complete OLD Array with old ORDER_ID and just show the new Arrays with new ORDER_ID – Bijan Zand Nov 25 '17 at 10:21
  • Have you tried something? You can start by looking up usort on php.net and then array_fillter – René Nov 25 '17 at 10:49
  • i am trying fix this problem with PHP or DB query or.... – Bijan Zand Nov 25 '17 at 10:57
  • Please explain *in the question(!)* when 2 entries are considered duplicates. What exactly makes them duplicate? Their `nid` value alone? Their `nid` and `title` combination? Their `title` alone? – trincot Nov 25 '17 at 18:14
  • I have an array (In fact, my array is derived query from the database DB_SELECT ) that some of them duplicated and all duplicate arrays are Same Title and Nid, but only Order_ID field is different and I want to avoid duplicate it with remove duplicate Arrays with just highest index ORDER_ID is kept. – Bijan Zand Nov 25 '17 at 18:48
  • look my array plan in Snippet http://sandbox.onlinephpfunctions.com/code/a7366d30cad7087f02362da0b2f4f1b0dd010a71 – Bijan Zand Nov 25 '17 at 18:57
  • Please provide desired output for that input. – trincot Nov 26 '17 at 21:04

1 Answers1

0

You can solve this by storing the data into an associative array keyed by nid values: if you do this in order of increasing Order_id you will be left with the most "recent" values per nid

If your data is in $array:

// Sort by Order_id
array_multisort(array_column($array, "Order_id"), $array);
// Keep one entry per nid
foreach ($array as $row) {
    $hash[$row["nid"]] = $row;
}
// Convert back to indexed array:
$result = array_values($hash);

See it run on eval.in

trincot
  • 317,000
  • 35
  • 244
  • 286
  • It works in the link I provided. Could you provide a link where it does not work, and explain me what is wrong with it? – trincot Nov 25 '17 at 18:12
  • In this method, the loop is repeated several times !! but work , how remove duplicate arrays ! – Bijan Zand Nov 26 '17 at 20:54
  • I don't understand what you are saying. Please provide a test case with input *and desired output*. – trincot Nov 26 '17 at 21:05
  • I would like to thank everyone for help me for fixing my problem ...my mistake is unclose While :D Thanks stackoverflow ! – Bijan Zand Nov 26 '17 at 21:22