Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3944078
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:51:28+00:00 2026-05-20T00:51:28+00:00

Cant really find any useful information on this through Google so hope someone here

  • 0

Cant really find any useful information on this through Google so hope someone here with some knowledge can help.

I have a set of results which are pulled from a multi dimensional array. Currently the array key is the price of a product whilst the item contains another array which contains all the product details.

key=>Item(name=>test, foo=>bar)

So currently when I list the items I just order by the key, smallest first and it lists the products smallest price first.

However I want to build on this so that when a user sees the results they can choose other ordering options like list all products by a name, certain manufacturer, colour, x ,y ,z etc etc from a drop down box(or something similar)

This is where I need some guidance. Im just not sure how to go about it or best practise or anything. The only way I can think of is to order all the items by the nested array eg by the name, manufacturer etc. but how do I do that in PHP?

Hope you understand what im trying to achieve(if not just ask). Any help on this with ideas, approaches or examples would be great.

Thanks for reading

p.s Im using PHP5

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:51:29+00:00Added an answer on May 20, 2026 at 12:51 am

    Updated example with a little snippet using in memory database 🙂
    P.S: the XSS protection in this example is not needed at all because I check the input as boolean value. To see results in reverse order you specify order?desc

    <?php
    
    /* XSS-protection. */
    $_GET   = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
    
    $array = array(
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
    );
    
    function createTable($db) {
        $db->exec("CREATE TABLE IF NOT EXISTS tags (id INTEGER PRIMARY KEY, tag TEXT NOT NULL UNIQUE)");
    }
    
    function insertData($db, $array) {
        $db->beginTransaction();
    
        foreach($array as $elm) {
            try {
                $stmt = $db->prepare("INSERT INTO tags (tag) VALUES (:tag)");
                $stmt->execute(array(
                    ":tag" => $elm
                ));
            } catch(PDOException $e) {
                /*** roll back the transaction if we fail ***/
                $db->rollback();
                /*** echo the sql statement and error message ***/
                echo $sql . '<br />' . $e->getMessage();
            }
        }
    
        $db->commit();
    }
    
    $db = new PDO('sqlite::memory:');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    //
    createTable($db);
    insertData($db, $array);
    
    $order = "ASC";
    if (strtoupper($_GET['order']) == "DESC") {
        $order = "DESC";
    }
    
    $stmt = $db->prepare("SELECT * FROM tags ORDER BY tag $order");
    $stmt->execute();
    
    $data = array();
    while($row = $stmt->fetch()) {  
        $data[] = array($row['tag']);
    }
    
    echo json_encode($data);
    

    Hope you understand what im trying to
    achieve(if not just ask). Any help on
    this with ideas, approaches or
    examples would be great.

    First I have a couple of questions you are saying that you are using PHP5. How do you retrieve your data(RDBMS)? If not, PHP5 has SQLite enabled by default. I think you should be using at least a RDBMS(SQLite/etc) to do the heavy lifting for you.

    When you learn SQL you don’t have to any sorting in PHP. I think this PDO tutorial while give you insides how to use SQL while doing it safely. SQL is vulnerable to SQL-injections but thanks to PDO’s prepared statements you don’t have to worry about that anymore.

    I have a set of results which are
    pulled from a multi dimensional array.
    Currently the array key is the price
    of a product whilst the item contains
    another array which contains all the
    product details.

    Use ORDER BY to order. I would use a datatable to do the sorting client-side. Also safes you to do work on the server(PHP). You could for example look at YUI2’s datatable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've done some research and I can't really find a preferred way to do
I've got a really simple rails question here but I can't seem to find
I'm really new to HTML, but I can't find anywhere how to return the
No doubt I'm missing something really simple here but I just can't see the
It's a really basic question but i can't think at the second. How do
Seems like as really simple thing to do, but I just can't track it
I cant seem to find much info on haskells layout features, as I understand
I cant post the code (proprietary issues) but does anyone know what types of
I cant call protected function in my base class. Why? It looks something like
Maybe i cant do what i want? I want to have 1 thread do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.