0

I have one multidimensional array as shown below(PHP) :

Array
(
    [144320] => Array
        (
            [3568728] => Array
                (
                    [30832] => 30832
                )

            [3568884] => Array
                (
                    [30827] => 30827
                    [30828] => 30828
                    [30830] => 30830
                    [30831] => 30831
                    [30832] => 30832
                    [30837] => 30837
                    [30838] => 30838
                    [30839] => 30839
                    [30826] => 30826
                    [30808] => 30808
                    [30806] => 30806
                    [30807] => 30807
                    [30698] => 30698
                    [30601] => 30601
                    [30697] => 30697
                )

        )

    [144330] => Array
        (
            [3568731] => Array
                (
                    [30827] => 30827
                    [30839] => 30839
                    [30838] => 30838
                    [30837] => 30837
                    [30832] => 30832
                    [30831] => 30831
                    [30828] => 30828
                    [30830] => 30830
                    [30826] => 30826
                    [30806] => 30806
                    [30808] => 30808
                    [30807] => 30807
                    [30698] => 30698
                    [30697] => 30697
                    [30601] => 30601
                )

        )

    [144218] => Array
        (
            [3568753] => Array
                (
                    [30808] => 30808
                )

        )

    [144216] => Array
        (
            [3568732] => Array
                (
                    [30808] => 30808
                )

        )

)

This array is populated by following code:

$sql = "SELECT * FROM `bf_alert_stack` WHERE `type` = 'immediately'  order by created desc";

$q   = db_query($sql);

$user_alerts = array();

  while ($row  = db_fetch_array($q)) 
{   
   $user_alerts [$row['uid']]    [$row['alert_id']]  [$row['nid']] = $row['nid'];
}

From the above user_alerts array I want to rearrange the [$row['nid']] array and for rearrangement I want capture [$row['nid']] array and after capturing it into another array I want to re-arrange $row['nid'] array I want to update this $row['nid'] array into original user_alerts array.

How I can do this? I am not getting any search for this on google so just placed this on appropriate place.

Akki
  • 1,718
  • 2
  • 29
  • 53
  • 6
    Please show the desired output from your example data – DaveRandom Apr 02 '13 at 12:08
  • Please describe what result you'd like and how your question differs from: http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2?rq=1 – Luceos Apr 02 '13 at 12:20
  • as far as re arranging $row['nid'] is concerned u can do that by using array_multisort($row['nid'], SORT_NUMERIC, SORT_DESC); var_dump($ar); but i am not sure what u mean by "I want to update this $row['nid'] array into original user_alerts array." – Ujwal Abhishek Apr 02 '13 at 13:08

1 Answers1

1

The best you can do is to get it ordered from the query with something like:

SELECT * FROM `bf_alert_stack` WHERE `type` = 'immediately' 
ORDER BY created desc, nid

But if you don't have access, or need the original sort for other reason, you need to iterate recursively over the array and reassign the last sortered level to the original array with PHP ksort method:

http://www.php.net/manual/en/function.ksort.php

foreach($user_alerts as $uid=>$user_alert) {
    foreach($alert as $alert_id=>$nids) {
        $user_alerts[$uid][$alert_id] = ksort($nids); 
    }   
}

Hope it helps!

milo-ft
  • 21
  • 1
  • 2