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.

3
  • 6
    Please show the desired output from your example data Commented Apr 2, 2013 at 12:08
  • Please describe what result you'd like and how your question differs from: stackoverflow.com/questions/2699086/… Commented Apr 2, 2013 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." Commented Apr 2, 2013 at 13:08

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

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.