1

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);
?>
2
  • 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). Commented Jul 12, 2011 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. Commented Jul 12, 2011 at 4:22

7 Answers 7

3

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);
Sign up to request clarification or add additional context in comments.

4 Comments

@Rachit - That would echo "'4'". You could make it do $user_id = $userdb["'William'"]; to get the user ID.
@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.
what if name has space like WIlliam Oxford or anyother special character? i used single quote to escape characters
@Rachit - You don't have to do that to store data in an array.
0

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

Comments

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

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

        }

1 Comment

it may be due to "'" you are using in loop. You need not to use them
0

Change

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

with this

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

Comments

0

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.

Comments

0

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);

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.