Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
?>
share|improve this question
you are adding extra quotes that are being stored inside the variable... not sure if you are doing this on purpose or not. $userdbname="'".$fqlResult[$i]['name']."'"; means that $userdbname will print as 'userName' (with the extra single quotes around it, and not just as userName). – T. Brian Jones Jul 12 '11 at 4:18
Not really related to the question, but keep in mind that if the names aren't unique, you could run into problems doing this. – Matthew Jul 12 '11 at 4:22

7 Answers

up vote 3 down vote accepted

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);
share|improve this answer
sorry i edited my question, i wanted to type search uid=4 – Rachit Jul 12 '11 at 4:13
1  
@Rachit - That would echo "'4'". You could make it do $user_id = $userdb["'William'"]; to get the user ID. – Francois Deschenes Jul 12 '11 at 4:14
@Rachit - I updated my answer. First, your User ID is wrapped in single quotes, that's why it's not working. See my first answer. To prevent that, see my second answer. – Francois Deschenes Jul 12 '11 at 4:20
what if name has space like WIlliam Oxford or anyother special character? i used single quote to escape characters – Rachit Jul 12 '11 at 4:21
@Rachit - You don't have to do that to store data in an array. – Francois Deschenes Jul 12 '11 at 4:22

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!";
}
share|improve this answer
sorry i edited my question, i wanted to type search uid=4 – Rachit Jul 12 '11 at 4:14
for($i=0;$i<count($fqlResult);$i++)
        {

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

        }
share|improve this answer
I have edited my question, it still doesnt works for me – Rachit Jul 12 '11 at 4:14
it may be due to "'" you are using in loop. You need not to use them – Gaurav Jul 12 '11 at 4:16

Change

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

with this

$userdb[$i] = "{$fqlResult[$i]['name']}";
share|improve this answer

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.

share|improve this answer

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);
share|improve this answer

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.