Michael
Michael

Reputation:

PHP - Foreach (Array of an Array)

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.

Upvotes: 0

Views: 2179

Answers (5)

Chester Alan
Chester Alan

Reputation: 81

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);
   }
}

Upvotes: 0

Toto
Toto

Reputation: 2430

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.

Upvotes: 0

Phill Pafford
Phill Pafford

Reputation: 85298

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?

Upvotes: 1

Michael
Michael

Reputation:

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

Upvotes: 0

GSto
GSto

Reputation: 42350

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?

Upvotes: 0

Related Questions