2 Small Array Questions (retrieving results)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NinjaBot
Forum Newbie
Posts: 7
Joined: Sat Apr 29, 2006 11:54 am

2 Small Array Questions (retrieving results)

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

play around with in_array(), array_search() and foreach
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

$key = array_search('home.php', $news); //returns index Home
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 :(
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply