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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:02:19+00:00 2026-06-17T21:02:19+00:00

Hi I want to make a good query to have a good array. Now

  • 0

Hi I want to make a good query to have a good array. Now for example I have this query:

SELECT DISTINCT * FROM products 
        LEFT OUTER JOIN  product_aliases 
        ON product_aliases.product_id = products.id 
        AND product_aliases.alias = '$alias'
        LEFT OUTER JOIN  (product_images
            LEFT OUTER JOIN  product_image_votes 
                    ON product_image_votes.product_image_id = product_images.id)
        ON product_images.product_id = products.id 
WHERE products.id = $id

The result is two array like this:

array(
(int) 0 => array(
    'products' => array(
        'id' => '1',
        'user_id' => '1',

    ),
    'product_aliases' => array(
        'id' => '1',
        'product_id' => '1',
        'language' => 'it',
    ),
    'product_images' => array(
        'id' => '1',
        'product_id' => '1',
    ),
    'product_image_votes' => array(
        'id' => '2',
        'product_image_id' => '1',
        'vote' => '1',
    )
),
(int) 1 => array(
    'products' => array(
        'id' => '1',
        'user_id' => '1',

    ),
    'product_aliases' => array(
        'id' => '1',
        'product_id' => '1',
        'language' => 'it',
    ),
    'product_images' => array(
        'id' => '1',
        'product_id' => '1',
    ),
    'product_image_votes' => array(
        'id' => '2',
        'product_image_id' => '1',
        'vote' => '1',
    )
)

The problem is that: I want only a unique array that contain product and for example product_images that contain in an array product_images_votes.
The first problem is:

  • have a unique array and not two arrays
  • create array inside array in base of my left join annidate

Example of array:

array(
(int) 0 => array(
    'products' => array(
        'id' => '1',
        'user_id' => '1',

    ),
    'product_aliases' => array(
        'id' => '1',
        'product_id' => '1',
        'language' => 'it',
    ),
    'product_images' => array(
        'id' => '1',
        'product_id' => '1',
        array('product_image_votes' => array(
            'id' => '2',
            'product_image_id' => '1',
            'vote' => '1',
        ))
    )

Is possible to do ?
I’m working with php

  • 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-17T21:02:20+00:00Added an answer on June 17, 2026 at 9:02 pm

    Your query is good as it is, but you need to construct the nesting in PHP. You cannot really produce nested structures in SQL alone so you must work with the flattened structure as you have it.

    This can be done with some creative looping. Create an output array which is indexed by products['id']. On each iteration, create a new entry if it does not already exist. If it does exist, add to its product_images, an array also indexed by product_images['id'].

    // To hold the final array
    $output = array();
    foreach ($original_array as $row) {
    
      // Storing these id values to make them easier to refer to...
      $id = $row['products']['id'];
      $pid = $row['product_images']['id'];
      $paid = $row['product_aliases']['id'];
      $pivid = $row['product_image_votes']['id'];
    
      // Create the product entry if it does not exist
      // and initialize arrays for product_images and product_aliases    
      if (!isset($output[$id])) {
        $output[$id] = array(
          'products' => $row['products'],
          // Initialize these to sub-arrays containing the first ones from this row
          // using the id as the array key
          'product_aliases' => array($paid => $row['product_aliases']),
          'product_images' => array($pid => $row['product_images'])
        );
        // Then add the first vote
        $output[$id]['product_images'][$pid]['product_image_votes'] = array();
        $output[$id]['product_images'][$pid]['product_image_votes'][$pivid] = $row['product_image_votes'];
      }
      // If it does exist already, append the alias if that does not exist, the image, the vote etc.
      else {
        // First add the alias if not already set
        if (!isset($output[$id]['product_aliases'][$paid])) {
          $output[$id]['product_aliases'][$paid] = $row['product_aliases'];
        }
        // Then add the image if not already set
        if (!isset($output[$id]['product_images'][$pid])) {
          $output[$id]['product_images'][$pid] = $row['product_images'];
        }
        // And nest in the image_votes
        if (!isset($output[$id]['product_images'][$pid]['product_image_votes'][$pivid])) {
          $output[$id]['product_images'][$pid]['product_image_votes'][$pivid] = $row['product_image_votes'];
        }
      }
    }
    

    There’s a lot here, and it is likely I have syntax errors or missing ] somewhere. Good luck with it.

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

Sidebar

Related Questions

I have a query like this: SELECT c.review, u.username, c.date, c.good, c.rate_id, r.rating FROM
I want to use Gama component in Android to make our app good.I have
I have link for example domain.com/de/controler/action?param=value and I want make actionlink to keep same
I not so good in Linq and I want to make a query to
Good day all, i have a load of images i want to display from
I have an array, and I want to make a hash so I can
I have this in Model: public function index_loop() { $post_user = $this->db->query(SELECT posts.post_title, posts.post_content,
I have example below: // start time // make mysql query here for($i =
I want make a bash script which returns the position of an element from
I have style sheet with a class name changebackgroundcolor i want make change in

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.