1

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?

2
  • But if you ignore the date for the uniqueness of the arrays, which date should be returned by arrayUnique-function for multioccurences? The first? The last? Average? Commented Oct 28, 2017 at 21:00
  • i want the same date that we are ignoring for the uniqueness .. maybe somehow we match the original array index with output array index and pull the date? I dont know .. I am stuck because if I remove the date from the arrays, then my results are all good.. if date is part of the arrays, my function takes every array is unique because of date. Commented Oct 28, 2017 at 21:02

1 Answer 1

1

Sure. I would use a function like:

function serializeWithout($array, $key = null) {
  if (isset($key)) unset($array[$key]);
  return serialize($array);
}

and replace the serialize call in your code with serializeWithout($item,'date');. Or you can add a $key parameter to your function an pass it through.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.