Search and remove an element in mulit-dimentional array in php depending on a criteria

Posted by Nadeem on Stack Overflow See other posts from Stack Overflow or by Nadeem
Published on 2013-10-18T20:34:05Z Indexed on 2013/10/18 21:55 UTC
Read the original article Hit count: 187

Filed under:
|

I've a simple question about multi-dim array, I want to remove any redundent element let's say in my case, [serviceMethod] => PL is coming 2 times, I want to search 'PL' with respect of [APIPriceTax] if an element has a lower price I want to keep it and remove the other one in array

Array (
    [0] => Array (
                   [carrierIDs] => 150
                   [serviceMethod] => CP
                   [APIPriceTax] =>  30.63 
                   [APIPriceWithOutTax]  28.32 
                   [APIServiceName] =>  Xpresspost USA 
                   [APIExpectedTransitDay]  => 2 
               )
    [1] => Array (
                    [carrierIDs] => 155
                    [serviceMethod] => PL
                    [APIPriceTax] => 84.13
                    [APIPriceWithOutTax] => 73.8
                    [APIServiceName] => PurolatorExpressU.S.
                    [APIExpectedTransitDay] => 1
               )
  [2] => Array (
                    [carrierIDs] => 164
                    [serviceMethod] => PL
                    [APIPriceTax] => 25.48
                    [APIPriceWithOutTax] => 22.35
                    [APIServiceName] => PurolatorGroundU.S.
                    [APIExpectedTransitDay] => 3
                  )

)

This is my pseudo code: Where $carrierAddedToList is the actual array

$newCarrierAry = function($carrierAddedToList)
  { 
   $newArray = array(); 
   foreach($carrierAddedToList as $cV => $cK) 
   { 
    if( !in_array($cK['serviceMethod'],$newArray) ) 
     { 
       array_push($newArray, $cK['serviceMethod']); 
     } 

   } 
    return $newArray;
 } ; 
  print_r($newCarrierAry($carrierAddedToList));

© Stack Overflow or respective owner

Related posts about php

Related posts about multidimensional-array