1

if i have a array

Array
(
[3] => Array
    (
        [0] => title = title dffgfghfdg
        [1] => 2-title2
        [2] => content = content 2
    )

[1] => Array
    (
        [0] => title = title erer
        [1] => 1-title1
        [2] => content = content 1
    )

[0] => Array
    (
        [0] => title = title sdfdf
        [1] => 4-title4
        [2] => content = content 4
    )

[2] => Array
    (
        [0] => title = titledfdf df
        [1] => 3-title3
        [2] => content = content 3
    )
) 

and i will make every [1] to be key. becouse i will sort the array within [1]..?

probably will be

Array
(
[2-title2] => Array
    (
        [0] => title = title dffgfghfdg
        [1] => 2-title2
        [2] => content = content 2
    )

[1-title1] => Array
    (
        [0] => title = title erer
        [1] => 1-title1
        [2] => content = content 1
    )

[4-title4] => Array
    (
        [0] => title = title sdfdf
        [1] => 4-title4
        [2] => content = content 4
    )

[3-title3] => Array
    (
        [0] => title = titledfdf df
        [1] => 3-title3
        [2] => content = content 3
    )
)

then i will sort with the keys? thanks

1
  • Your question is a little unclear. Are you asking how to sort the array with the non-integer keys? Commented Jan 2, 2012 at 2:22

2 Answers 2

3

Use a foreach() to transform the array, then use ksort().

foreach($a as $k => $v) {
   $b[$v[1]] = $v;
}
ksort($b);

Something like that.

0
3

Use usort for it. http://php.net/manual/en/function.usort.php

function user_cmp( $a, $b )
{
    if( $a[1] == $b[1] ) return 0;
    return ($a[1] < $b[1]) ? -1 : 1;
}

$arr = array( ... );
usort( $arr, 'user_cmp' );
1
  • +1, but I would simplify user_cmp to just return strcmp($a[1], $b[1]);. Commented Jan 2, 2012 at 16:25

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.