user1299028
user1299028

Reputation: 315

How do i reference an array as a whole?- php

array_unique is to be used by passing the array name as a parameter.

But as far as all the online examples researched go, it shows the arrays first being assigned to a single variable at the time they are instantiated, like so:

$var1 = $array['val1', 'val2', 'val3']

The only problem is, i am assigning the values of my array 1 at a time via a loop, so i dont know how to assign my array as a whole to a variable.

So how can i represent the entire array to either put in a variable or pass as a parameter for array_unique directly, without having to reference the specific array values?

Edit: Added the loop where array values are instantiated, as requested.

$productsQueryResult = mysql_query($productsQuery);
        while ($row = mysql_fetch_row($productsQueryResult))
            {
                $array[$i] = $row[0];
                $i++;
            }

Upvotes: 0

Views: 58

Answers (4)

Vinay
Vinay

Reputation: 2594

Try this.

$productsQueryResult = mysql_query($productsQuery);
$temp = array ();
while ($row = mysql_fetch_row($productsQueryResult))
{
    //process $row value
    array_push($temp, $row[0]); 
}

$newarray = array_unique($temp);

Upvotes: 0

Kalpesh Patel
Kalpesh Patel

Reputation: 2822

Simply pass name of the array as parameter:

array_unique( $array );

Upvotes: 1

tcak
tcak

Reputation: 2212

$productsQueryResult = mysql_query($productsQuery);

$array = [];

while ($row = mysql_fetch_row($productsQueryResult))
{
    $array[] = $row[0];
}

$unique_array = array_unique( $array );

Upvotes: 0

Karan Punamiya
Karan Punamiya

Reputation: 8863

While looping, you can append the value as the last element of the array as:

loop-statement
{
    ...

    $arr[] = value;

    ...
}

Then, you can use the $arr variable in any array processing functions.

Upvotes: 0

Related Questions