I have a multidimensional array like this
Array
(
[0] => Array
(
[name] => test
[c1] => flower
[c2] => fruit
[date] => 2017-10-05 10:44:05
)
[1] => Array
(
[name] => test
[c1] => flower
[c2] => fruit
[date] => 2017-10-06 10:44:08
)
[2] => Array
(
[name] => test1
[c1] => chicken
[c2] => fruit
[date] => 2017-10-07 10:44:10
)
[3] => Array
(
[name] => test2
[c1] => flower
[c2] => cow
[date] => 2017-10-08 10:44:15
)
)
So I am using this function to select unique arrays from a multidimensional array ( ref: http://phpdevblog.niknovo.com/2009/01/using-array-unique-with-multidimensional-arrays.html ) This link has the answer to why I am not using php function array_unique() as well.
function arrayUnique($array, $preserveKeys = true)
{
// Unique Array for return
$arrayRewrite = array();
// Array with the md5 hashes
$arrayHashes = array();
foreach($array as $key => $item) {
// Serialize the current element and create a md5 hash
$hash = md5(serialize($item));
// If the md5 didn't come up yet, add the element to
// to arrayRewrite, otherwise drop it
if (!isset($arrayHashes[$hash])) {
// Save the current element hash
$arrayHashes[$hash] = $hash;
// Add element to the unique Array
if ($preserveKeys) {
$arrayRewrite[$key] = $item;
} else {
$arrayRewrite[] = $item;
}
}
}
return $arrayRewrite;
}
but if date is part of the arrays, the above function fails because every array has different date and then it considers each of them unique. Is there a way to avoid date being used in the above function but in output still get unique arrays with date?