Jump to content

Mysql_Query Loop Into Multidimensional Array


chachew

Recommended Posts

So i am trying to get a list of items out of my MySql database but my loop is not creating the array correctly, its dropping multiple rows when the key is the same name.

 

<?php
$search = 'item1';

$con = require_once('./dbconnect.php');

mysql_select_db("packages", $con);
$package = mysql_query("SELECT * FROM $search", $con);

while($item = mysql_fetch_row($package)){
$arr[$item[1]] = array('description' => $item[2], 'image' => $item[3]);
}


echo json_encode($arr);
mysql_close($con);
?>

 

So the loop works and does actually create the array with the various 'packages' in their respective array. The issue that i am having is that mysql_fetch_row($package)) can and will contain multiple keys that are the same name but with different things in the array.

 

For example, the database will be something like this:

PACK LEV DESCRIPTION IMAGES

item1 A1 description for stuff1 image1

item1 A2 description for more stuff image2

item1 B1 more stuff here image3

 

item2 A1 description here for item2 image1

item2 B1 description contents here image2

---------------------------------------------------------------------------------

 

My Above code will produce an array like this:

Array
(
[item1] => Array
(
[description] => more stuff here
[image] => image3
)

[item2] => Array
(
[description] => description contents here
[image] => image2
)

)

 

What i want the array to be is:

Array
(
[item1] => Array
(
[A1] => Array
(
[description] => description for stuff1
[image] => image1
)


[A2] => Array
(
[description] => description for more stuff
[image] => image2
)


[b1] => Array
(
[description] => more stuff here
[image] => image3
)


[item2] => Array
(
[A1] => Array
(
[description] => description here for item2
[image] => image1
)


[b1] => Array
(
[description] => description contents here
[image] => image2
)
)


)

 

I hope this makes sense..Thanks for looking

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.