0

I want to sort array in php based on some conditions here is the array

 $array = [
    [ "a"  => 4, "b" => 8, "c" => 1 ],
    [ "a"  => 9, "b" => 4, "c" => 0 ],
    [ "a"  => -9, "b" => -4, "c" => 1 ],

];

if c=1,it should come first,how can i sort the array like this? I need to add some additional conditions too..i think the same logic will helps me to add multiple conditions

2
  • Your example array has no valid syntax,plz show real given and real expected data here. Commented Mar 13, 2017 at 14:54
  • Ok,i edited the question @-JustOnUnderMillions Commented Mar 13, 2017 at 15:13

2 Answers 2

2

You can sort by a column of the array by doing something like:

<?php
$array = [
    [ "a"  => 4, "b" => 8, "c" => 1 ],
    [ "a"  => 9, "b" => 4, "c" => 0 ],
    [ "a"  => -9, "b" => -4, "c" => 1 ],

];

array_multisort($array, array_column($array,"c"));
print_r($array);

This will sort by the column "c".

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

2 Comments

Can you please consider my additional question-@apokryfos
@ubm that's a completely different question to the original one so you should ask a separate question with all necessary details.
0

You can use http://php.net/manual/en/function.usort.php, to create your own sorting-logic or you can use http://php.net/manual/en/function.asort.php, which sorts your array by values.

To get an overview of php sorting functions use this page: http://php.net/manual/en/array.sorting.php

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.