-2

I have array A :

Input:

A ={2,3,2,1,3,2,0,3,2,0,11,7,9,2}

I want output to be Array B

Output:

B={0,1,2,3,7,9,11}
user3636123
  • 41
  • 2
  • 7
  • 2
    Hint: [`array_unique()`](http://php.net/manual/en/function.array-unique.php) and [`sort()`](http://php.net/manual/en/function.sort.php) – jensgram Sep 11 '14 at 07:45
  • _number increase sort_ -- what does that mean? Do you mean _sort numbers in increasing order_? – Barmar Sep 11 '14 at 07:48

5 Answers5

1

Your friends are: array_unique() and sort()

With these functions you can create the distinct list and sort it:

$a = {2,3,2,1,3,2,0,3,2,0,11,7,9,2}
$b = array_unique($a, SORT_NUMERIC)
sort($b, SORT_NUMERIC)
Pred
  • 8,789
  • 3
  • 26
  • 46
0

You have to filter/sort by distinct number values and order ascending..

Unique values: Select only unique array values from this array

Order values: http://www.w3schools.com/php/php_arrays_sort.asp

Community
  • 1
  • 1
Revils
  • 1,478
  • 1
  • 14
  • 31
0

You need to get all unique values and then sort them. array_unique() removes all duplicate values. sort() sorts your values from low to high.

$B = array_unique($A);
sort($B);
TeeDeJee
  • 3,756
  • 1
  • 17
  • 24
  • `sort` returns a boolean, not a new array. Its argument has to be a variable, not an expression, because it takes a reference. – Barmar Sep 11 '14 at 07:49
  • @Barmar You're absolutely right. I posted my answer a bit to fast. Edited answer. – TeeDeJee Sep 11 '14 at 07:50
0
//remove duplicate values from array using array_unique
$unique_array = array_unique($a);
//sort the resulting array
sort($unique_array);
//dump it to verify
var_dump($unique_array);
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
0

Input:

A ={2,3,2,{1},3,2,{0},3,2,0,11,7,9,{2}} I want output to be Array B

Output:

B={0,1,2,3,7,9,11}

user3636123
  • 41
  • 2
  • 7