0

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.

4
  • 2
    So what happens instead? Commented Oct 2, 2009 at 14:24
  • 1
    What is the expected output and what is the actual output? Commented Oct 2, 2009 at 14:33
  • let me guess, you forgot semicolon at the end of your array_unique line? Commented Oct 2, 2009 at 14:54
  • The output on my local server prints out a unique list of names and a unique list of items they viewed. Commented Oct 2, 2009 at 15:29

5 Answers 5

1

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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?

3 Comments

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".
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]; }
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.
0

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

Comments

0

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.

Comments

0

Try this one:

    $mstorage = array();
    foreach ($marray as $e){
    if(in_array($email, $mstorage) === FALSE) {
        echo "Name: ". $e[0]."<br />";
        echo "Email: ". $e[1]."<br />";
        array_push($mstorage, $email);
   }
}

Comments

Your Answer

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