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 8551533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:13:00+00:00 2026-06-11T14:13:00+00:00

Is it possible to make a multilevel array using a mysql query? E.g. if

  • 0

Is it possible to make a multilevel array using a mysql query? E.g. if I want to get 4 pictures for each product?

[1] => Array( 'name' => 'Product 1', 'picture' => array('picture1','picture2','picture3','picture4') )
[2] => Array( 'name' => 'Product 2', 'picture' => array('picture5','picture6','picture7','picture8') )

Or do I need to make a foreach to loop through the products and then in the foreach make a mysql query to get each products pictures?

EDIT:

My structure is:

P_Attributes
--------
id, int(15)
name, varchar(256)


P_AttributeValues
--------
id, int(15)
value, varchar(256)
attribute_id, int(15) [NOTE: This is connected to P_Attributes.id]

Then I want to get ALL P_AttributeValues to a P_Attribute row – and get it in ONE query. Is that possible?

EDIT 2:

With the query made by the accepted answers author I made it work with this PHP-code:

$attributevalues = $auctionClass->get_rows($id);

$attr_val = array();
foreach($attributevalues as $k => $v){
    $attr_val[$v->AID]['attr_name'] = $v->AName;
    $attr_val[$v->AID]['parameters'][] = array('attr_value_name' => $v->name, 'id' => $v->id);
}
  • 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-06-11T14:13:01+00:00Added an answer on June 11, 2026 at 2:13 pm

    If you just use a normal JOIN query and order it, you can get rows coming out of the records that can be formed in to the necessary hierarchical structure.

    SELECT pa.id AS product_id, pa.name , pav.id AS product_attr_id, pav.value
    FROM P_Attributes pa JOIN P_AttributeValues pav ON pav.attribute_id = pa.id
    ORDER BY pa.id ASC, pav.id ASC
    

    This will generate rows like this:

    product_id, name, product_attr_id, value
    1, "Product 1", 1, "picture1"
    1, "Product 1", 2, "picture2"
    1, "Product 1", 3, "picture3"
    1, "Product 1", 4, "picture4"
    2, "Product 2", 5, "picture5"
    2, "Product 2", 6, "picture6"
    2, "Product 2", 7, "picture7"
    2, "Product 2", 8, "picture8"
    

    No idea what MySQL extension you are using, presumably PHP as I mentioned it and you didn’t correct me. If you fetch the associative array per record returned, which will be, per record, in this form:

    array('product_id' => 1, 'name' => "Product 1",
          'product_attr_id' => 1, 'value' => "picture1");
    

    If you have a main data array called $products, you can produce it by just putting this code in the loop, assuming the record is called $productRec and filled in the loop before this.

    if (!array_key_exists($productRec['product_id'], $products)) {
        $products[$productRec['product_id']] = array('name' => $productRec['name'],
                                                     'picture' => array());
    }
    
    $products[$productRec['product_id']]['picture'][$productRec['product_attr_id']] =
        $productRec['value'];
    

    Using the IDs for the keys should be alright, presuming that they are primary keys with no duplicates. Will aid look up that way, rather than losing that data.

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

Sidebar

Related Questions

when using a makefile, I want the following to be possible: make clean make
Is it possible make a CCSprite to black and white one using setBlendFunc ?
Possible Duplicate: Make Browser Window Blink in Task Bar Hello I want to notify
Is possible to make stylish for browse button <input type=file /> using CSS and
Is it possible to make an Nhibernate query generate Sql that has columnName<>'value' rather
Is it possible to make the corners of a thumbnail rounded using css? EDIT
Is it possible to make AJAX-calls (e.g. using jQuery.ajax() ) from local html/js file
Is it possible to make a site with ASP.NET MVC Framework using .NET 2.0?
Is it possible to make such buttons ( http://img225.imageshack.us/img225/6452/buttonslw9.jpg ) using CSS? It should
Is it possible to make an array with two data types in android programming.

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.