Home > Software engineering >  search for a name inside array that it's inside array
search for a name inside array that it's inside array

Time:05-23

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'),
);
?>

CodePudding user response:

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

        }
}

CodePudding user response:

$x = 0;
$pSize = count($persons);
foreach ($persons as $key => $data)
    { 
        $x  ;
        if (trim($data) == $userInput)
        { 
            echo "Found";
            break;
        }elseif($pSize - $x == 0){
            echo "Not found";
        }
    }
  • Related