简体   繁体   中英

PHP array_search not working

can anybody let me know why array_search doesnt works for me? All i want is to search value and and get corresponding key value for eg if i search wiliam i should get 4.. Its simple but aint working for me

<?php
$fqlResult[0]['uid']='1';
$fqlResult[0]['name']='Jay';
$fqlResult[1]['uid']='2';
$fqlResult[1]['name']='UserName2';
$fqlResult[2]['uid']='3';
$fqlResult[2]['name']='Frances';
$fqlResult[3]['uid']='4';
$fqlResult[3]['name']='William';



        for($i=0;$i<count($fqlResult);$i++)
        {

            $userdbname="'".$fqlResult[$i]['name']."'";
            $userdb[$userdbname]="'".$fqlResult[$i]['uid']."'"; 

        }


echo "<pre>";
print_r($userdb);
echo "</pre>";
echo array_search('4', $userdb);
?>

It doesn't work because array_seach searches values and "William" is a key. To complicate things, your values and keys are wrapped in single quotes during the for loop.

You'd want to do something like this:

if ( ! empty($userdb["'William'"]) )
{
  // Echoes "'4'"
  echo $userdb["'William'"];
}

// To find user ID "'4'"
// Outputs "'William'"
echo array_search("'4'", $userdb);

If you don't want things wrapped in single quotes , you'll need to change your for loop as follows:

for($i=0;$i<count($fqlResult);$i++)
{
  $userdbname=$fqlResult[$i]['name'];
  $userdb[$userdbname]=$fqlResult[$i]['uid']; 
}

if ( ! empty($userdb["William"]) )
{
  // Outputs "4" (without the single quotes)
  echo $userdb["William"];
}

// To find user ID "4" (without the single quotes)
// Outputs "William"
echo array_search('4', $userdb);

array_search() searches values, not keys.

If you want to check the existence of something that you use as a key in an array, you can just use isset:

if(isset($userdb['William'])) {
    echo "$userdb[William] is William's uid!";
}
for($i=0;$i<count($fqlResult);$i++)
        {

            $userdbname=$fqlResult[$i]['uid'];
            $userdb[$userdbname]=$fqlResult[$i]['name']; 

        }

Change

$userdb[$userdbname]="'".$fqlResult[$i]['uid']."'";

with this

$userdb[$i] = "{$fqlResult[$i]['name']}";

array_search only works with arrays of scalar data. You're trying to search an array of arrays. You can easily search the array yourself:

function search_array_col($array, $col, $val)
{
   foreach ($array as $key => $a)
   {
     if ($a[$col] == $val) return $key;
   }

   return null;
}

echo search_array_col($fqlResult, 'name', 'William') , "\n";
echo search_array_col($fqlResult, 'uid', '4') , "\n";

Edit: n/m, I misread your code. However, you could still use this to search your original array, so I'll leave the answer for reference.

try this:

foreach($fqlResult as $result)
{
    $name = $result["name"];
    $uid = $result["uid"];
    $userdb[$name] = $uid;
}

then you want to use array_key_exists() to find the key. array_search() only works for searching values, not keys.

$nameExists = array_key_exists("William",$userdb);

You can remove the quotes in the $userdbname="'".$fqlResult[$i]['name']."'"; rewrite it to

$userdbname= $fqlResult[$i]['name'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM