PHP multidimensional array from database results

13,695

Is the top level an associative array or a numeric array?

If it is an associative array, it should have structure like this:

$unique_array = array(
    'username'=>array('John','Mike',...),
    'user_id'=>array(1,2,3,...),
    'week_number'=>array(1,2,3,...)
);

Or if it is a numeric array, it should have structure like this:

$unique_array = array(
    array('username'=>'John', 'user_id'=>1, 'week_number'=>1),
    array('username'=>'Mike', 'user_id'=>2, 'week_number'=>2),
    array('username'=>'Sam', 'user_id'=>3, 'week_number'=>3),
    ...
)

for the first type use the code below:

while ($row = mysql_fetch_assoc($query)) {
    $unique_array['username'][] = $row['username'],
    $unique_array['user_id'][] = $row['user_id'],
    $unique_array['week_number'][] = $row['week_number'],
}

for the second type, it is something like your code. But there are some syntax problems:

while($row = mysql_fetch_array($query)) //yes, I know mysql is deprecated
{
    $unique_array[] = array('username'=>$row['username'], 'user_id'=>$row['user_id'], 'week_number'=>$row['weeknumber']);
}
Share:
13,695
RobDubya
Author by

RobDubya

I use php lots, but I'm branching out into more UI stuff. I am a family man and a lucky husband. I also Can't stand stupid home wrecking whores or people that don't respect theirselves. My life revolves being a better person than I was yesterday.

Updated on June 05, 2022

Comments

  • RobDubya
    RobDubya almost 2 years

    I'm a bit new to multidimensional arrays, and would like to see if I'm doing it right. preferably, I'd like to name the arrays within the main array for ease of use.

    $unique_array = array(
        username=>array(),
        user_id=>array(),
        weeknumber=>array()
        );
    

    and then I have a while loop which checks some database results:

    while($row = mysql_fetch_array($query)) //yes, I know mysql is deprecated
    {
    $unique_array[] = username=>$row['username'], user_id=>$row['user_id'], week number=>['weeknumber'];
    }
    

    I'm not sure if I am placing the values in the array from within the while loop correctly, or if it needs to be done some other way. I couldn't find any resources I could easily understand on SO or elsewhere to deal with query results within a named array within a multidimensional array.

    EDIT FOLLOW UP QUESTION: I also need to check the array for duplicate values, because there will be multiple values that are exactly the same, but I only want one of them.

    Any help is appreciated!

    EDIT SOLUTION:

    By modifying the answer I was able to create code to fit my needs.

    Array initialization:

    $unique_array = array(
        'username'=>array(),
        'user_id'=>array(),
        'weeknumber'=>array()
        );
    

    Building the array from within a while loop:

    while($row = mysql_fetch_array($query))
    {
    $unique_array[] = array('username'=>$row['username'], 'user_id'=>$row['user_id'], 'weeknumber'=>$row['weeknumber']);
    }
    

    And finally, I need to make sure the array values are unique (there are duplicates entries as a result of database and query limitations), after the while loop I have:

    print_r(multi_unique($unique_array));