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
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 itsproduct_images, an array also indexed byproduct_images['id'].There’s a lot here, and it is likely I have syntax errors or missing
]somewhere. Good luck with it.