1

I have three arrays like this:

$a1  = array('55','something1','something2','something3' );
$a2  = array('77','something14','something25','something36' ); 
$a4  = array('66.6','something15','something25','something34' );

and a array of these three like this:

$a  = array($a1,$a2,$a4 );

I know by using sort() I can sort numeric values but here what I want to sort above arraysin such a manner that sorting occurs only through first numeric element of arrays $a1,$a2 and $a4 i.e. output should be first $a1 because 55 less than 66.6 and 77 then $a4 because 66.6 is less than 77 and the $a2.I dont't want values of elements in arrays $a1 ,$a2,$a4 get changed only array $a should be rearranged nummerically.

2

1 Answer 1

1

using usort

usort — Sort an array by values using a user-defined comparison function

$a1  = array('55','something1','something2','something3' );
$a2  = array('77','something14','something25','something36' ); 
$a4  = array('66.6','something15','something25','something34' );


$array = [$a1, $a2, $a4];

usort($array, function($a, $b) {
    if ($a[0] > $b[0]) {
        return 1;
    } else {
        return -1;
    }
});


print_r($array);

live example: https://3v4l.org/QnUXc

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

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.