Home > Net >  array_search returns just 1 result
array_search returns just 1 result

Time:08-24

I´m starting to study PHP and I have a doubt in one lesson about array_search.

The teachers example shows "A peeple who got a 10" and as only one of the people got a 10 everything works fine, showing the person's name.

But I've been trying to use it to fetch more than one result. In this case I created this array, with 3 people taking 10:

    $notas2=array(
    "Ana"=>4,
    "Misca"=>10,
    "Chatuba"=>6,
    "Jurandir"=>7,
    "Musca"=>10,
    "Mickey Mouse"=>10,
);

echo array_search(10,$notas2);

This code just returns "Misca". I tried a foreach, but it returned only "MiscaMiscaMiscaMiscaMiscaMisca". lol

   foreach(array_search(10,$notas2)as $tirou10){
    echo $tirou10;
}

Anyone can help-me?

Tanks.

CodePudding user response:

array_search will export only first corresponding key. If you want to get all people who got 10 use array_keys($notas2, 10) it will return array and instead of echo use var_dump() or var_export()

CodePudding user response:

The PHP function array_search only returns the first entry.

  • Related