Tuesday, September 27, 2011

Remove the Duplicate sub-Array element using PHP

/*
 * Function to Remove the Duplicate sub-Array element.
 */
function removeDuplicateArraybyElement(&$searchArray, $searchKey){
 
  /* Sorting the array values */
  objectSort($searchArray, $searchKey);
 
  /* Removing duplicate array values*/
  $searchBackupValue = "";
  $output = array ();
  foreach ($searchArray as $key => $sub_array){
      if ($searchBackupValue != $sub_array->$searchKey){
          $output[] = $sub_array;
      }
      $searchBackupValue = $sub_array->$searchKey;
  }
 
  $searchArray = $output;
}

Example with Drupal Code:

$products = taxonomy_get_tree(13, 0, -1, 1);
removeDuplicateArraybyElement($products, 'name');

No comments: