i have two tables category and images.i want create a multidimensional array like

$headings = ['heading 1', 'heading 2', 'heading 3'];
$images = [
    'heading 1' => ['image 1', 'image 2', 'image 3', 'image 4', 'image 5'],
    'heading 2' => ['image 1', 'image 2', 'image 3'],
    'heading 3' => ['image 1']
];

So how can i do that using php.

Images table

enter image description here

category table

enter image description here

share|improve this question
    
title careate correct to create – Turtle Oct 1 '16 at 10:36
1  
what have you tried? It seems like you want us to develop the whole system for you stackoverflow.com/questions/39804655/… – RST Oct 1 '16 at 10:37

The tool code you will need: mysqli. Now suppose you know it.

Solution 1

The slowest. Just fetch all the lines you need, then use a for loop to make it what you want.

Solution 2

Use GROUP_BY + GROUP_CONCAT function in sql.

I am in a hurry. So which do you like best, and do you need a further example?

share|improve this answer
$sql = "select category.name,group_concat(images.image_name) from category inner join images on (category.id = images.category_id) group by category.name";
$table = mysql_query($sql);
$arr = array();
while($row = mysql_fetch_row($table))
{
    $row['image_name'] = explode(",", $row['image_name']);
    $arr[] = $row;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.