vote up 0 vote down star

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.

flag
2  
So what happens instead? – Gumbo Oct 2 at 14:24
1  
What is the expected output and what is the actual output? – Randell Oct 2 at 14:33
let me guess, you forgot semicolon at the end of your array_unique line? – SilentGhost Oct 2 at 14:54
No, semicolons are there. – Michael Oct 2 at 15:29
The output on my local server prints out a unique list of names and a unique list of items they viewed. – Michael Oct 2 at 15:29

4 Answers

vote up 0 vote down

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?

link|flag
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 Oct 2 at 14:46
could you post the code that is using array_unique? – GSto Oct 2 at 14:58
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 Oct 2 at 15:10
On my local server this works fine. Both servers on php 5.2 – Michael Oct 2 at 15:28
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 Oct 2 at 16:00
show 1 more comment
vote up 0 vote down

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

link|flag
vote up 0 vote down

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?

link|flag
vote up 0 vote down

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.

link|flag

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.