2

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));
2
  • Which way are you trying to grow the array, by fields or by rows? Commented Jul 1, 2013 at 2:58
  • By fields - I think I have it now! I'll post the code in a bit. Commented Jul 1, 2013 at 17:44

1 Answer 1

6

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']);
}
Sign up to request clarification or add additional context in comments.

1 Comment

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()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.