Jump to content

mysql multidimensional array


Destramic

Recommended Posts

hey guys, i have a bit of a strange question and dont know if what im asking can actually be done with mysql but here goes.

 

basically i have a table like so:

 

items

--------------------

item_id

category

sub_category

sub_sub_category

name

price

---------------------

 

now what im after is a result like so:

 

category           sub_cat             sub_sub_cat

[consoles] => [accessories] => [headsets] => [0] => ('name' => 'item1',

                                                                                   'price' => '10.00')

 

now ive seen a query which converts to json (but didnt work)

but would be great if its possible to do what i want it to do

SELECT 
     CONCAT("[",
          GROUP_CONCAT(
               CONCAT("{username:'",username,"'"),
               CONCAT(",email:'",email),"'}")
          )
     ,"]") 
AS json FROM users;

any advise would be great

 

thank you

 

Link to comment
Share on other sites

ok thanks

 

but is it possible to get my results from the database like so:

 

category           sub_cat             sub_sub_cat

[consoles] => [accessories] => [headsets] => [0] => ('name' => 'item1',

                                                                                               'price' => '10.00')

 

 

Link to comment
Share on other sites


$sql = "SELECT item_id
, category
, sub_category
, sub_sub_category
, name
, price
FROM items
ORDER BY category, sub_category, sub_sub_category";
$res = $db->query($sql);
$data = array();
while (list($id, $cat, $subcat, $subsubcat, $name, $price) = $res->fetch_row()) {
$data [$cat][$subcat][$subsubcat][$id] = ['name'=>$name, 'price'=>$price];
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.