What is the best way to arrange a three-dimensional array according to a particular index?

Despite countless searches on Google, I am still struggling to figure out how to sort a 3-dimensional associative array.

Specifically, my goal is to arrange each user's alert jobs based on the job's posted date.

Here is the code snippet:

$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'];
}

In the $user_alerts array, $row['nid'] contains specific jobs for each user's alert.

When a user receives an email alert, the jobs should be displayed in sorted order based on the date.

The sample data from the $user_alerts array is shown below:

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

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

        )

    [144330] => Array
        (
            [3568731] => Array
                (
                    [30827] => 30827
                    [30839] => 30839
                    [30838] => 30838
                    [30837] => 30837
                    [30832] => 30832
                    [30831] => 30831
                    [30830] => 30830
                    [30828] => 30828
                    [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
                )

        )

)

I aim to sort the above user_alerts array based on row['nid'], but the details of row['nid'] are stored in a database table. All nid values should be rearranged according to the creation date of nid present in the table.

Answer №1

It may seem a little confusing, but there's a chance that this solution will work..

$sql = "SELECT * FROM `bf_alert_stack` WHERE `type` = 'immediately' ORDER BY `created` DESC, `uid` ASC, `alert_id` DESC, `nid` DESC";

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using PHP's exec() function to manipulate character encoding via the command line

I've been struggling to pass UTF-8 text as an argument to a command line program using the exec function in PHP. It seems like there are some issues with character encoding causing trouble. When I run locale charmap from the terminal, it returns: UTF ...

Reorganizing Array from PHP after Decoding Facebook Open Graph

I'm in the process of creating an app that would greatly benefit from suggesting a user's Facebook friends as they type. However, I've hit a roadblock when it comes to converting the Open Graph result (retrieved by accessing a user's fr ...

Invalid file format for product images in Magento version 1.9.0.1

Every time I try to upload an image in the product option, Magento 1.9.0.1 gives me an error saying "Disallowed file format." ...

Using Selenium to identify and recognize different color patterns

Is it feasible to use Selenium (preferably in combination with PHPUnit Selenium) to detect colors on a webpage? I have banners on my site, and some of them do not reload properly. It's strange because I can access their elements, but all I see on the ...

Creating an HTML table with a PHP loop

I executed a MySQL query to populate an array: if ($result) { $foundResult = true; foreach ($result as $row) { array_push($searchResultAccount,$row->Account); array_push($searchResultUsername,$row->Username); array_pu ...

PHP DateTime difference format returns negative zero

I created a function that checks if the difference between the current date and another date is less than x days. Below is the code for my function : private function notBeforeDate($date_to_check, $interval) { $now = new DateTime('now'); ...

How can I format time display as shown in Laravel 5.2?

I am currently working with Laravel 5.2 and I would like to display the creation time of articles in a specific format: created_at displaying in 1 day today 2-10 days ...

Navigate through the JSON dataset

I am struggling with looping out JSON-data in an HTML list using PHP. The structure of the JSON data is as follows: { "msg": [ "msg text 1", "msg text 2", "msg text 3", "msg text 4", "msg text 5", "msg text 6" ] } My current P ...

Having issues with the Facebook messenger bot webhook functionality

My Facebook messenger bot, built in PHP, is functioning well when I send messages to my page. However, it seems that the webhook of the bot does not work when other users (besides myself, who is the owner of the page) send messages to my page. I would app ...

PHP is causing disruptions to HTML

Every time PHP code is present in my file, the page appears blank and upon checking the source code, there are no tags visible on the page. However, once I remove the PHP code, the form displays correctly without any issues. <?php $title = $_POST[&apos ...

Why do we use array[] instead of just array when creating a new stdClass object?

$arr = []; $arr[] = new stdClass; //this adds an object to the array $arr = new stdClass; //this changes arr into an object It's peculiar because $arr was initially declared as an array. If you remove the brackets in $arr = new stdClass; then $arr ...

Retrieve unique values for each day out of a total of 35 days using PHP

I am working on a project where I need to calculate the daily ROI value for 35 days and save it in the user table. Here is the code snippet that I am using: $total_days = 35; $total_amount = 200; $arr = array(); for($i = 0; $i < $total_days; ++$i) { ...

Is it possible to trigger an AJAX function automatically following the success of another AJAX function?

I am in the process of implementing a note system using procedural PHP and AJAX. This system should have the ability to display new notes without refreshing the page and load more notes dynamically without needing a full page refresh. Currently, each func ...

Can Joomla components be displayed without using iframe or plugins?

Is there a way to showcase a component view without the use of iframes or plugins? Perhaps through PHP and SQL queries? UPDATE: Just to clarify: I am looking to accomplish this directly in the PHP template! (It would also be helpful to do it within an art ...

Setting up Algolia integration in Laravel

While attempting to install the Algolia Laravel package, I encountered an error stating: The trait 'App\AlgoliaEloquentTrait' cannot be found I followed the installation, configuration, and quickstart instructions provided in this link: h ...

Setting up a Laravel project on a DSO handler server

I am currently working on a Laravel 5.0 project that needs to be tested on servers supporting different PHP handlers such as: 1. suPHP (Single user PHP) 2. FCGI (FastCGI) 3. CGI (Common Gateway Interface) 4. DSO (Dynamic Shared Object) The project ru ...

The attempt to fetch the submitted data via the PHP URL was unsuccessful

I have a form and I've created a URL to fetch data. The data is being fetched properly, but when I try to access the URL, it shows {"error":"null"}. How can I retrieve the submitted value? I am having trouble displaying the web services as I attempt t ...

Despite displaying a 'Couldn't fetch mysqli_stmt' warning, the insert query successfully transmits the data to the database

I have a form on my website that contains two dropdown lists. Both of these dropdowns are populated with values from the database tables. When a visitor submits the form, certain actions are triggered: A SELECT query is used to compare the selected choic ...

Exploring the Functionality of Anchors in Regular Expressions

The symbol "^" indicates that the match must start at the beginning of a line or string. Can you explain what is meant by the term "string" in this context? If we use the expression /^(apple)/, will it match the phrase this applesauce is delicious, since ...

Dealing with unsuccessful ajax responses

My ajax request is not handling errors correctly. Earlier, the "r" was <br /> <b>Warning</b>: simplexml_load_string() [ <a href='function.simplexml-load-string'> function.simplexml-load-string and it did not ...