Skip to content

Instantly share code, notes, and snippets.

@alancoleman
Last active December 17, 2015 16:09
Show Gist options
  • Save alancoleman/5636496 to your computer and use it in GitHub Desktop.
Save alancoleman/5636496 to your computer and use it in GitHub Desktop.
Function to walk through a multidimensional array and remove any successive duplicates based on an index
<?php
// Function to walk through a multidimensional array and remove any successive duplicates based on an index
function stripDuplicateHeader(&$stripArray, $index){ // Pass a reference of the array to be processed and the index to check
$newArr = array(); // Create an array in which to populate the processed arrays
foreach (array_reverse($stripArray) as $val) { // walk through the array to be processed backwards
$newArr[$val[$index]] = $val; //populate
}
$stripArray = array_reverse(array_values($newArr)); // remove duplicates
return $stripArray; // Return array
}
// Call function to strip duplicate headers
$header = stripDuplicateHeader($header, 'field');
//var_dump($header);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment