2

I have a variable which is an array of arrays

$janvier[]=array( 'type', 'date');

I want to sort it following the date so I used this code

$janvier=> $janvier->sortby($janvier['date'])

but it shows me this error:

call to a member function sortby() on array

Couldn't find what's wrong

I'm so used to low level languages this is my first time using a high level language

2 Answers 2

1

You can create a custom function for this case:

array_sort_by_column($array, 'date');

function array_sort_by_column(&$array, $column, $direction = SORT_ASC) {
    $reference_array = array();

    foreach($array as $key => $row) {
        $reference_array[$key] = $row[$column];
    }

    array_multisort($reference_array, $direction, $array);
}

For more you can check this question

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

Comments

1

sortBy is a collection method from laravel, you can't use it on a array.

If you want to sort the array by the key data use this code:


$janvier = array_multisort(array_values($janvier), SORT_DESC, array_keys($janvier), SORT_ASC, $janvier);

Look at the array_multisort method for more info

1 Comment

Thanks for taking time to help but this just made the content of the array to NULL

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.