1

So, I am getting unique values from my MD array utilizing the following function:

    function unique_multidim_array($array, $key) { 
        $temp_array = array(); 
        $i = 0; 
        $key_array = array();           
        foreach( $array as $val ) { 
            if ( ! in_array( $val[$key], $key_array ) ) { 
                $key_array[$i] = $val[$key]; 
                $temp_array[$i] = $val; 
            }
            $i++; 
        } 
        return $temp_array; 
    } 

My array is similar to the following:

Array (
    [0] =>
        Array (
            'name' => 'Nevada'
        )
    [1] =>
        Array (
            'name' => 'Colorado'
        )
    [2] =>
        Array (
            'name' => 'Nevada'
        )
    [3] =>
        Array (
            'name' => 'Colorado'
        )
    [4] =>
        Array (
            'name' => 'Oklahoma'
        )
    [5] =>
        Array (
            'name' => 'Nevada'
        )
    [6] =>
        Array (
            'name' => 'Nevada'
        )
)

And using the function (unique_multidim_array ( $term_arr, 'name' )) above I am getting a single Nevada and a single Colorado, however, it is not returning Oklahoma

What can I do to ensure that it will return unique values, even if there are no duplicates?

10
  • What PHP version? It's working for me here Commented Aug 25, 2017 at 15:55
  • I can't reproduce your problem: phpfiddle.org/main/code/k8in-xv05 Commented Aug 25, 2017 at 15:55
  • php7 on a Lemp box Commented Aug 25, 2017 at 15:56
  • Not sure if you can view this... but it's in use here: getme.onl/portfolio/multi-family the tabs should show Colorado, Nevada, Oklahoma, California Commented Aug 25, 2017 at 15:57
  • 1
    How are you iterating over the resulting array? Depending on how it might be of no importance, but do notice that the indices are preserved, Oklahoma's array in the resulting array is 4. Commented Aug 25, 2017 at 15:58

2 Answers 2

3

Your resulting array keeps the original indices, and, depending on how you are iterating over it, you might get unexpected results. Try resetting the indices:

function unique_multidim_array($array, $key) { 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array();           
    foreach( $array as $val ) { 
        if ( ! in_array( $val[$key], $key_array ) ) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[] = $val; // <--- remove the $i
        }
        $i++; 
    } 
    return $temp_array; 
}

Or, as you say, array_values() will help too:

$term_arr = array_values ( unique_multidim_array ( $term_arr, 'name' ) );
Sign up to request clarification or add additional context in comments.

Comments

1

PHP already has a function to remove duplicates from an array

array_unique(array)

should do the trick

3 Comments

aye, but it didn't because of the mulitdimensionalness of it
array_map("unserialize", array_unique(array_map("serialize", $array))) could do it...
According to the docs: "Two elements are considered equal if and only if (string) $elem1 === (string) $elem2". So it can't works for multidimensional array The serialize/unserialize option could works but looks dirty.

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.