News:

Join the Facebook Fan Page.

Main Menu

Array Search

Started by DoctorToxn, June 01, 2005, 03:06:48 PM

Previous topic - Next topic

DoctorToxn

Hi again.

I need help.  I am srting to search an array with the Search_Array function.  I have a flat file that I import into an array with the File function.  I can open the file into an array just fine.  But, when I try to search the array,  it comes up blank.

Here is the Flat File (usernames.data.php):
admin
carlo



Here is the code:
<?php

// Open the file into an array
$lines file("usernames.data.php");
print_r($lines);
echo 
"<BR><BR>";

// search array for string
$key array_search('admin'$lines);
 
print_r($key);

?>


The first Print_R works fine and echos the array as follows:
Array ( [0] => admin [1] => carlo )
But the second print r finds nothing.  I need an anwser.

Thanks
BuilderDan

Thantos

its blank because its returning a value of 0.  print_r won't help you as it returns the key and not an array.  Try this:

$key 
array_search('admin'$lines);
if ( 
$key === false )
  echo 
'Not found';
else
  echo 
"$key";

DoctorToxn

Ok, but why is it not returining the array line?  Even tough it is in the array?

BuilderDan

Thantos

because array_search returns the key not the value. http://www.php.net/manual/en/function.array-search.php

If you want the value you can then do:
$lines[$key]

DoctorToxn

But I am wanting it to return the key.  And as you say it is returing "0", why is it not echoing "0"? 

I am sry if i am being a pain.  I am just wanting to learn this stuff.

Thantos

print_r won't print an integer 0 it assumes it means empty.  The value is really 0 so you just have to deal with it.

Advertisement: