-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}
2
  • 2
    Hint: array_unique() and sort() Commented Sep 11, 2014 at 7:45
  • number increase sort -- what does that mean? Do you mean sort numbers in increasing order? Commented Sep 11, 2014 at 7:48

5 Answers 5

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)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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);

Comments

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);

2 Comments

sort returns a boolean, not a new array. Its argument has to be a variable, not an expression, because it takes a reference.
@Barmar You're absolutely right. I posted my answer a bit to fast. Edited answer.
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}

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.