Hello ,
I have an 2 dimetional array:

Array (
[0] =>Array ( [0] => add1 [1] => add2 [2] => May 2014 [3] => ascascascasc [4] => 160 )
[1] =>Array ( [0] => Framework [1] => hii [2] => May 2014 [3] => ascascasc [4] => 161 )
[2] =>Array ( [0] => OS [1] => other OS skill [2] => May 2014 [3] => ascascASC [4] => 162 )
[3] =>Array ( [0] => Databases [1] => test db skill 3 [2] => May 2014 [3] => bnmbnmbnm [4] => 158 ) 
[4] =>Array ( [0] => Framework [1] => test framework 1 [2] => May 2014 [3] => asdasdasd [4] => 159 ) 
      ) 

i want to sort this array.
I searched on google about 2 dimentional arry search,
but I got solutions for key-value pair (where is string).
like Array (

    [0] =>Array ( ['type1'] => add1 ['type2'] => add2 ['type3'] => May 2014 ['type4'] => ascascascasc ['type5'] => 160 ))

Recommended Answers

All 10 Replies

Member Avatar for diafol

I'm not sure what you mean by sort. Could you give us an idea of what the output from a successful sort would look like?

ohh am sorry
output should be like

    Array (
    [0] =>Array ( [0] => add1 [1] => add2 [2] => May 2014 [3] => ascascascasc [4] => 160 )
    [1] =>Array ( [0] => Databases [1] => test db skill 3 [2] => May 2014 [3] => bnmbnmbnm [4] => 158 )
    [2] =>Array ( [0] => Framework [1] => test framework 1 [2] => May 2014 [3] => asdasdasd [4] => 159 )
    [3] =>Array ( [0] => OS [1] => other OS skill [2] => May 2014 [3] => ascascASC [4] => 162 )
    ) 

sorted with 0'th index of array

@iamthwee:
thanks!
I had tried that too.
In my case,above given array $new_array
so I wrote like:

$ar = usort($new_array, "cmp");
var_dump($ar);

where cmp function is function cmp($a, $b) { return $a[0] - $b[0];}
but nothing :(
it just print bool(true)

Member Avatar for diafol

If this is your data...

$data = array (
    array ('add1','add2','May 2014','ascascascasc',160),
    array ('Framework','hii','May 2014','ascascasc',161 ),
    array ('OS','other OS skill','May 2014','ascascASC',162 ),
    array ('Databases','test db skill 3','May 2014','bnmbnmbnm',158 ), 
    array ('Framework','test framework 1','May 2014','asdasdasd',159 ) 
); 

This is all you need...

$first = array();
foreach ($data as $k => $r) $first[$k]  = $r[0];
array_multisort($first, SORT_FLAG_CASE | SORT_STRING, $data);

To see the output...

echo "<pre>";
print_r($data);
echo "</pre>";
Member Avatar for diafol

Here's a simple class with static method for sorting...

class diaSort
{
    public static function msort($data, $key, $desc = false, $sortFlags='SORT_FLAG_CASE,SORT_STRING')
    {
        $total = 0;
        if($sortFlags)
        {
            $flags = explode(',',$sortFlags);
            foreach($flags as $flag) $total += constant(trim(strtoupper($flag)));
        }
        $sortDir = ($desc == true) ? 3 : 4; //SORT_DESC = 3, SORT_ASC = 4 
        $first = array();
        foreach ($data as $k => $r) $first[$k]  = $r[$key];
        array_multisort($first, $sortDir, $total, $data);
        return $data;
    }
}

$data = array (
    array ('add1','add2','May 2014','ascascascasc',160),
    array ('Framework','hii','May 2014','ascascasc',161 ),
    array ('OS','other OS skill','May 2014','ascascASC',162 ),
    array ('Databases','test db skill 3','May 2014','bnmbnmbnm',158 ), 
    array ('Framework','test framework 1','May 2014','asdasdasd',159 ) 
); 

$newData = diaSort::msort($data,0);

echo "<pre>";
print_r($newData);
echo "</pre>";

The flags by which you can sort (4th parameter) are:

'SORT_REGULAR' - compare items normally (don't change types)
'SORT_NUMERIC' - compare items numerically
'SORT_STRING' - compare items as strings
'SORT_LOCALE_STRING' - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
'SORT_NATURAL' - compare items as strings using "natural ordering" like natsort()
'SORT_FLAG_CASE' - combined with 'SORT_STRING' or 'SORT_NATURAL' ['SORT_FLAG_CASE,SORT_STRING' or 'SORT_FLAG_CASE,SORT_NATURAL'

This is modified from http://www.php.net/manual/en/function.array-multisort.php

AWESOME @diafol !!!
I liked it!

but just a small requirement (again) :(
It is not taking case sensative.
How can I do that?
array get printed

array ('Databases','test db skill 3','May 2014','bnmbnmbnm',158 ),
array ('Framework','hii','May 2014','ascascasc',161 ),
array ('Framework','test framework 1','May 2014','asdasdasd',159 )
array ('OS','other OS skill','May 2014','ascascASC',162 ),
array ('add1','add2','May 2014','ascascascasc',160),
array ('database','add3','May 2014','ascascascasc',160),

but I want

array ('add1','add2','May 2014','ascascascasc',160),
array ('database','add3','May 2014','ascascascasc',160),
array ('Databases','test db skill 3','May 2014','bnmbnmbnm',158 ),
array ('Framework','hii','May 2014','ascascasc',161 ),
array ('Framework','test framework 1','May 2014','asdasdasd',159 )
array ('OS','other OS skill','May 2014','ascascASC',162 ),
Member Avatar for diafol

4th parameter -

$newData = diaSort::msort($data,0,false,'SORT_FLAG_CASE,SORT_STRING');
echo "<pre>";
print_r($newData);
echo "</pre>";
Member Avatar for diafol

Ah. I see. I know it's a pain, but I would strongly suggest using the latest version of php if that is an option. php 5.5.0 was released about a year ago and it's disappointing to see many hosts (including the Google App Engine!) still using 5.4 or earlier.

The article to which you point has a neat solution for transforming the key to lowercase (or uppercase for that matter). I'm a bit busy at the moment, but I'll see if I can jiggle the code to take 5.4- into account.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.