2

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

2

2 Answers 2

2
$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; } 
    1

    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?

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.