0
Array
(
)
Array
(
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
)
Array
(
    [0] => 14
    [1] => 12
)

I want this array

Array
(
    [0] => 14
    [1] => 12
)

Here is my code:

$colorarray = array();
foreach($catIds as $catid){
    $colorarray[] = $catid;
}

Need to get unique array values

Thanks

3
  • All those arrays to begin with, are they inside another array? Or are they entirely separate values? Commented May 20, 2019 at 7:14
  • Look like this is multi-dimension array - that way I answered with array flatten first Commented May 20, 2019 at 7:38
  • I have modified your foreach loop please check my answer. Commented May 20, 2019 at 7:55

6 Answers 6

3

You can use call_user_func_array with array-merge for flatten your array and then use array-unique as:

$res = call_user_func_array('array_merge', $arr);
print_r(array_unique($res)); // will give 12 and 14

Live example 3v4l

Or as @Progrock suggested: $output = array_unique(array_merge(...$data)); (I like that syntax using the ...)

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

1 Comment

Or: $output = array_unique(array_merge(...$data));
1

You can always do something like this:

$colorarray = array();
foreach($catIds as $catid){
    if(!in_array($catid, $colorarray) {
       $colorarray[] = $catid;
    }
}

But also this has n*n complexity, So if your array is way too big, it might not be the most optimised solution for you.

Comments

1

You can do following to generate unique array.

array_unique($YOUR_ARRAY_VARIABLE, SORT_REGULAR);

this way only unique value is there in your array instead of duplication.

UPDATED

This is also one way to do same

<?php
 // define array 
 $a = array(1, 5, 2, 5, 1, 3, 2, 4, 5); 

 // print original array 
 echo "Original Array : \n"; 
 print_r($a); 

 // remove duplicate values by using  
 // flipping keys and values 
 $a = array_flip($a); 

 // restore the array elements by again  
 // flipping keys and values. 
 $a = array_flip($a); 

 // re-order the array keys 
 $a= array_values($a); 

 // print updated array 
 echo "\nUpdated Array : \n "; 
 print_r($a); 
?>

Reference link

Hope this will helps you

1 Comment

This answer ignores the (different) sample data in the question.
1

I have updated your code please check

$colorarray = array();
foreach($catIds as $catid){
    $colorarray[$catid] = $catid;
}

This will give you 100% unique values.

1 Comment

I recommend using this adjustment to your original code. It has the lowest possible time complexity and makes no iterated function calls. When the loop finishes, you can call array_values() on $colorarray to remove the temporary keys.
0

PHP: Removes duplicate values from an array

<?php
$fruits_list = array('Orange',  'Apple', ' Banana', 'Cherry', ' Banana');
$result = array_unique($fruits_list);
print_r($result);
?>

------your case--------

  $result = array_unique($catIds);
    print_r($result);

Comments

0

You can construct a new array of all values by looping through each sub-array of the original, and then filter the result with array_unique:

<?php
$data =
[
    [
        0=>13
    ],
    [
        0=>13
    ],
    [
        0=>17,
        1=>19
    ]
];

foreach($data as $array)
    foreach($array as $v)
        $all_values[] = $v;

var_export($all_values);
$unique = array_unique($all_values);
var_export($unique);

Output:

array (
    0 => 13,
    1 => 13,
    2 => 17,
    3 => 19,
  )array (
    0 => 13,
    2 => 17,
    3 => 19,
  )

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.