Page 1 of 1

2 Small Array Questions (retrieving results)

Posted: Sun Apr 30, 2006 3:21 pm
by NinjaBot
Not had luck finding information on how to do this, so maybe someone can shed some light.

Code: Select all

$getcurrentpage = $_SERVER['PHP_SELF'];
$i = 0;
$news = array(
	"Home" => "home.php",
	"Products" => "products.php",
	"News" => "news.php",
	"Contact" => "contact.php"
);

How would I write something to work like follows (where products.php, news.php and contact.php could be selected from the array):

Code: Select all

if ($getcurrentpage equal to ($news[1] to $news[3])){
	echo "Non-Home Page\n";
}
Is it also possible to return the array number for an array item? for example:

Code: Select all

while($news = each($news)){
	if ($news["value"] == $getcurrentpage){
		$page = $news[$i]; //get number of array item
	}
	$i++;
}
echo $page;
Thanks for any help.

Posted: Sun Apr 30, 2006 3:52 pm
by feyd
play around with in_array(), array_search() and foreach

Posted: Mon May 01, 2006 2:43 am
by dibyendrah
$key = array_search('home.php', $news); //returns index Home

Posted: Mon May 01, 2006 5:01 am
by s.dot
Is it also possible to return the array number for an array item?
Have a look at array_keys() using the optional search_value

[edit] Or, array_search(), like feyd said. My reading sucks tonight :(