views:

466

answers:

4

Here is an example:

for($i=1; $i < 10; $i++){
  $marray[] = array($name, $email, $password); // Lets just say for now, there is real
                                               // data for each online being input
}

foreach ($marray as $e){
   echo "Name: ". $e[0];
   echo "Email: ". $e[1];
}

I forgot to mention: This script works fine on both my servers. But, When I include array_unique before "Foreach" is called, it doesn't work, no error message or anything.

A: 

I would check which version of PHP both servers have installed. you can do this with phpinfo(). I can't think of any other reason it would work on one server, but not another. What do you mean by 'doesn't recognize?' do you get any error messages? If so, what are they?

GSto
Actually I forgot to mention. That script up there works fine. When I use "array_unique", that's when it fails. I don't get any error message, It just "foreachs" one record instead of the example up there "10 records".
Michael
could you post the code that is using array_unique?
GSto
Here is a cut out: $q = mysql_query($strSQL); $hold = array(); $view = array(); while($rez = mysql_fetch_array($q)) { $view[] = array($rez[6],$rez[1],$rez[8],$rez[9]); $hold[] = array($rez[2],$rez[3],$rez[6]); } $hold = array_unique($hold); sort($hold); $view = array_unique($view); sort($view); foreach($hold as $u){ echo $u[0]; }
Michael
On my local server this works fine. Both servers on php 5.2
Michael
array_unique casts whatever is in the array (in this case, another array) as a string. it's not really designed to work with arrays of arrays, you may want to write your own custom function. Also, it's possible that there is some difference in the mySQL data that is causing the problem.
GSto
Well the MYSQL is from the same host / db / table..
Michael
A: 

Welp, figured this one out my self. Instead of making it more confusing, I just set the initial text in an "exploded" matter.

Michael
A: 

Works fine for me:

$name = "Phill";
$email = "[email protected]";
$password = "p@ssw0rd";

for($i=1; $i < 10; $i++){
  $marray[] = array($name, $email, $password); 

}

foreach (array_unique($marray) as $e){
   echo "Name: ". $e[0]."<br />";
   echo "Email: ". $e[1]."<br />";
}

This is returned:

Name: Phill
Email: [email protected]

What version of PHP are you using?

Phill Pafford
A: 

As read on the php documentation:

Note: Note that array_unique() is not intended to work on multi dimensional arrays.

(Ugly) fix if "email + lastname + email" is the key:

$uniqueUsers = array();
foreach ($users as $user) {
    $uniqueUsers[$user['name'] . $user['lastname'] . $user['email']] = $user;
}

I think it is better to build directly (if possible) the array without duplicates.

Toto