-2

I want to sort my array according to idai value. I've tried to use SORT,KSORT,USORT but nothing useful.

Here is my data

Array
(
[0] => Array
    (
        [0] => Array
            (
                [idai] => 4
                [id] => 6187
                [name] => xyz 
            )

        [1] => Array
            (
                [idai] => 5
                [id] => 5256
                [name] => abc
            )

        [2] => Array
            (
                [idai] => 10
                [id] => 21921
                [name] => qwe 
            )

        [3] => Array
            (
                [idai] => 6
                [id] => 29679
                [name] => IOU
            )

        [4] => Array
            (
                [idai] => 11
                [id] => 21062
                [name] => STU
            )

    )

)

And I'm not sure why I'm getting this nested array.. Here is my how I declared my array:

$return_arr = array();
$return_arr['feed'] = array();

My code to store data in array from my db

    $query = "SELECT * FROM user_post WHERE userid = '$friend_id'";
    $result = mysql_query($query);


    while( $row = mysql_fetch_array($result) ) {

        $row_array['idai'] = $row['id'];
        $row_array['id'] = $row['post_id'];
        $row_array['name'] = $pic['name'];

        array_push($return_arr['feed'],$row_array);

      }         
deejay
  • 572
  • 4
  • 18
  • Remove $return_arr = array(); to remove the nested array – momouu Dec 13 '15 at 16:46
  • here is the answer: http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value – pmaniora Dec 13 '15 at 16:52
  • @pmaniora nothing changes.. its still same array. I've gone through some other similar solutions aswell.. but nothing helpful. – deejay Dec 13 '15 at 17:28
  • how you load this table? can you show full code? – pmaniora Dec 13 '15 at 17:34
  • @pmaniora i'll update my question.. – deejay Dec 13 '15 at 17:44
  • Why don't you sort in your query instead? It makes no sense to fetch the results unsorted and then sort in PHP. Why not simply sort using `ORDER BY`? – h2ooooooo Dec 13 '15 at 17:49
  • ahm.. its a bit complicated, the approach that i'm using. its takes 2 or more friend ids and then fetch all data of 1 id at first and then second id. now if i use ORDER BY, it will sort the data for each id but not for both ids id#1 Data 1, 5 7 id#2 Data 2,4,6 now at the end i'll get 1,5,7,2,4,6 .. I hope you understand my issue.. – deejay Dec 13 '15 at 18:03

1 Answers1

0

Try this code:

$return_arr['feed'][$row['id']] = $row_array;

or if you want not nested array:

$return_arr[$row['id']] = $row_array;

it will automaticly sort as array key is id

pmaniora
  • 76
  • 5
  • don't know whats problem with my code.. nothing is working.. its showing same old array.. no sorting nothing.. – deejay Dec 13 '15 at 18:44