3

I have one multidimensional array and this array I have to convert into single array with sort. I tried to use call_user_func_array('array_merge', $ranges); this function. After using this function get single array. When I use sort() function on single array then show output 1.

My array

    Array
(
    [range1] => Array
        (
            [0] => 1113
            [1] => 2224 
        )

    [range2] => Array
        (
            [0] =>  500
            [1] => 1112
        )

    [range3] => Array
        (
            [0] => 2225
            [1] => 4446
        )
)

Use call_user_func_array('array_merge', $ranges); output

    Array
(
    [0] => 1113
    [1] => 2224 
    [2] =>  500
    [3] => 1112
    [4] => 2225
    [5] => 4446
)

Now I am use sort() function then show 1. Why this happen?

2 Answers 2

2

You're almost there. In sort() function, array is passed by reference, and return true on success and false on failure. So you should apply sort() function on the flattened array like this:

$array = call_user_func_array('array_merge', $ranges);
sort($array);

// display sorted array
var_dump($array);

Here's the reference:

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

Comments

1

sort() function work by reference and returning true or false so no need to asign result to variable because source variable is changed by reference

2 Comments

so how can i sort from lowest value to highest value
@dhanashri sorted array is the same variable as this one provided to sort function.

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.