4

how to sort array which is the same with datebase data?

I requested google analytics datas, the datas is an larget array , I want to join the array with some other fields from my local database , then I extend the large array again. Now I want to sort the large array which is the same with using my sql like this:

select * from ga_table where ...
select * from ga_table order by age
select * from ga_table group by name

my array now is like:

$arr=array(
    'header'=>array('name','age','money'),
    'values'=>array(
        array('jimmy',20,30),
        array('tina',18,12),
        array('darcy',19,50),
    )
);

but now I had a large array not a db table , then how to sort the array ? enter image description here

6
  • 4
    Maybe add in how you want it sorted? Name, values? Commented Oct 29, 2013 at 10:02
  • 1
    Something like PHPLinq Commented Oct 29, 2013 at 10:03
  • take a look here Commented Oct 29, 2013 at 10:06
  • PHP has built in sort functions but it depends on what you want to do exactly Commented Oct 29, 2013 at 10:07
  • How you want it to be sorted? by name or age or money ? Commented Oct 29, 2013 at 10:09

3 Answers 3

9

you can try array_multisort: http://php.net/manual/en/function.array-multisort.php

foreach ($arr['values'] as $key => $row) {
    $name[$key]  = $row[0];
    $age[$key] = $row[1];
    $money[$key] = $row[3];
}

now if you want to sort by name in ASC you can:

array_multisort($name, SORT_ASC, $arr['values']);

or by name DESC:

array_multisort($name, SORT_DESC, $arr['values']);

or age ASC:

array_multisort($age, SORT_ASC, $arr['values']);

or age DESC and name ASC

array_multisort($age, SORT_DESC, $name, SORT_ASC, $arr['values']);
Sign up to request clarification or add additional context in comments.

Comments

1
 asort(); 
 arsort();
 krsort(); 

etc.. all are PHP inbuilt functions used to sort the array.

For better understanding you can visi the link http://php.net/manual/en/array.sorting.php

Comments

0

Thanks to @mamdouh alramadan!

I wrote a helper function to sort array like MySQL

function arrayOrderBy(array $items, array $orderBy)
{
    if (empty($items) || empty($orderBy)) {
        return $items;
    }

    $multisortArgs = [];

    foreach ($orderBy as $field => $direction) {
        $columnValues = array_column($items, $field);

        if (empty($columnValues)) {
            continue;
        }

        $multisortArgs[] = $columnValues;
        $multisortArgs[] = strtoupper($direction) === 'ASC' ? SORT_ASC : SORT_DESC;
    }

    if (empty($multisortArgs)) {
        return $items;
    }

    $multisortArgs[] = &$items;

    call_user_func_array('array_multisort', $multisortArgs);

    return $items;
}

Usage

$data[] = array('volume' => 67, 'edition' => 7);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 2);

$sorted = arrayOrderBy($data, ['volume' => 'desc', 'edition' => 'asc']);

print_r($sorted);

And the output is:

Array
(
    [0] => Array
        (
            [volume] => 98
            [edition] => 2
        )

    [1] => Array
        (
            [volume] => 86
            [edition] => 1
        )

    [2] => Array
        (
            [volume] => 86
            [edition] => 6
        )

    [3] => Array
        (
            [volume] => 85
            [edition] => 6
        )

    [4] => Array
        (
            [volume] => 67
            [edition] => 2
        )

    [5] => Array
        (
            [volume] => 67
            [edition] => 7
        )

)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.