0

I want to find a specific name given by user in this array if it's not found show a message. I tried but no result some help please.

<?php
$persons= array(
  array('name'=>'ADIANE','password'=>'adiane45'),
array('name'=>'ASABAN','password'=>'asaban23'),
array('name'=>'BENKASSOU','password'=>'benkassou67'),
);
?>
feitan
  • 3
  • 5

2 Answers2

0

Try this one, i hope this code will work:

$x = 0;
$pSize = count($persons);

foreach($my_array as $number => $number_array)
{
    $x++;
    foreach($number_array as $data = > $user_data)
        {
            print "Array number: $number, contains $data with $user_data.  <br>";
            if (trim($data) == $userInput)
            { 
                echo "Found";
                break;
            }elseif($pSize - $x == 0){
                echo "Not found";
            }

        }
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 23 '22 at 08:25
  • What is `$my_array`? Shouldn't that be `$persons`? And why do you have a nested loop instead of just using `$number_array['name']`? – Barmar May 23 '22 at 13:46
  • And shouldn't `$data` be `$user_data`? – Barmar May 23 '22 at 13:46
  • Thank you with this code I'm able to find the specific name but if I tried to find the name and the password of it I can't. by using '&&' in the 'if' – feitan May 23 '22 at 13:55
  • $x = 0; $pSize = count($persons); foreach($persons as $number => $number_array){ $x++; foreach($number_array as $data => $user_data){ if ($user_data == trim($nameInput) && $passInput == $user_data) { echo "Bienvenue Monsieur $nameInput"; break; }elseif($pSize - $x == 0){ echo "Votre login et/ou mot de passe n'est pas reconnu."; break; } } }}` – feitan May 23 '22 at 13:58
  • this is the code that I used – feitan May 23 '22 at 13:58
0

this is the answer thank you all for help.

$nameInput = $_POST['name']; $passInput = $_POST['password'];

        $nameInput = strtoupper($nameInput);
        $c=0;
        foreach($persons as $key) {
            if($key['name'] == trim($nameInput) && $key['password'] == $passInput){
                $c+=1;
                break;
            }
        }
        if ($c==1){
            echo "Bienvenue Monsieur $nameInput ";
        }else{
            echo "Votre login et/ou mot de passe n'est pas reconnu.";
        }
feitan
  • 3
  • 5