Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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));
share|improve this question
    
Which way are you trying to grow the array, by fields or by rows? –  Ignacio Vazquez-Abrams Jul 1 '13 at 2:58
    
By fields - I think I have it now! I'll post the code in a bit. –  RobDubya Jul 1 '13 at 17:44
add comment

1 Answer

up vote 2 down vote accepted

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|improve this answer
    
It's an associative array, but I don't know exactly what will fill it. So for something like 'username'=>array('John','Mike',...) I don't know who John or Mike will be, or how many there will be. Can I just leave it as 'username'=>array()? –  RobDubya Jul 1 '13 at 3:41
add comment

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.