Inferno Web Development
  • Home
  • Forums
  • About
  • Links
  • Contact

Tutorials List

  • C++ Tutorials

Articles

  • Java
  • PHP
  • Photoshop
  • C#
  • C++ Qt GUI
  • C++ Win32 API
  • C++
  • MASM32
  • General News
  • JavaScript
  • Web Development
  • Windows Tweaks

Popular Tutorials

All time:

  • Website Header with 3D Fold Up / Lift Effect
  • C++ Win32 API Tutorial
  • Perfect C++ String Explode Split
  • Simple C++ DLL Loading a Message Box
  • Simple C++ Pointers and References

Related Articles

  • Global Scope

Programming Tutorials

Home

PHP IndexOf Key like JavaScript

Submitted by Baran Ornarli on Tue, 08/12/2008 - 23:21.
  • PHP

Many beginners to PHP wonder about how they use to do something in another language. One question that comes up often is, how do you use indexOf like in JavaScript in PHP? Well you can either use the following code or array_search function.

The following function is just like the indexOf function in JavaScript. In fact, there is no PHP equivalent.

Simply enter your needle or the value of the array you are looking for and the array haystack to search in. Example: $ArrayIndexNumber = indexOf($thisValue, $BigArray);

function indexOf($needle, $haystack) {
	for($i = 0,$z = count($haystack); $i < $z; $i++){
		if ($haystack[$i] == $needle) {  //finds the needle
			return $i;
		}
	}
	return false;
}

The alternative is to use PHP's built-in array_search() function. However, array_search only returns the Key value, not the Key index number.

$array = array('fruit_1' => 'orange', 'fruit_2' => 'apples', 'fruit_3' => 'awesome_fruit');
$keyName = array_search('apples', $array); // $keyName is 'fruit_2' 
// do not expect array_search to work the same as indexOf.


5
Average: 5 (1 vote)
»
  • Share Tutorial

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <img> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h2>
  • Lines and paragraphs break automatically.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • Table of contents based on the <h*> tags

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
 ooooo         ooooooo  ooooo  oooooo   oooooo     oooo               oooooooooo.                
`888' `8888 d8' `888. `888. .8' `888' `Y8b
888 Y888..8P `888. .8888. .8' ooo. .oo. 888 888 ooo. .oo.
888 `8888' `888 .8'`888. .8' `888P"Y88b 888 888 `888P"Y88b
888 .8PY888. `888.8' `888.8' 888 888 888 888 888 888
888 o d8' `888b `888' `888' 888 888 888 d88' 888 888
o888ooooood8 o888o o88888o `8' `8' o888o o888o o888bood8P' o888o o888o


Enter the code depicted in ASCII art style.

Navigation

  • Home
  • Forums
  • Image Gallery
  • Links
  • About
  • Contact

Why Register? Contribute articles and tutorials to ID, earn titles, learn from ID programming projects, and advertise your blog in our forums.

  • Forum Community
  • Register Now
  • Write an Article


Copyright © Inferno Development 2008-2009. All Rights Reserved.