Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 29-06-2009
Member
 
Join Date: Jun 2009
Posts: 312
Position of PHP Array

I think that its fairly simple to add an element to an array in PHP but the problem is how do I determine the position of an element in a PHP array ?

Also i would like to know how can i find the position of the PHP Array ? Please help me guys..
Reply With Quote
  #2  
Old 29-06-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: Position of PHP Array

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

Use array_search( ). It returns the key of the found element or false:
Quote:
$position = array_search($array, $value);
if ($position !== false) {
// the element in position $position has $value as its value in array $array
}
Reply With Quote
  #3  
Old 29-06-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Position of PHP Array

key() returns the index element of the current array position.If you want to get the key name by position from Array than this will be the script:
Quote:
<?php
$myArray['name1']=3;
$myArray['name2']=2;
$myArray['name3']=1;

echo($myArray[1]); /* return NULL */
/* isset($myArray[1]) return false; */
/* is_null($myArray[1]) return true; */

function KeyName($myArray,$pos) {
// $pos--;
/* uncomment the above line if you */
/* prefer position to start from 1 */

if ( ($pos < 0) || ( $pos >= count($myArray) ) )
return "NULL"; // set this any way you like

reset($myArray);
for($i = 0;$i < $pos; $i++) next($myArray);

return key($myArray);
}

echo KeyName($myArray,1); // result: name2
echo KeyName($myArray,2); // result: name3
echo KeyName($myArray,3); // result: "NULL"
?>
Reply With Quote
  #4  
Old 29-06-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Position of PHP Array

However, because array_search () elegant studies in which the value is not found, it' s better to use array_search () instead of in_array ().
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Position of PHP Array"
Thread Thread Starter Forum Replies Last Post
how can I know the position on a document? Genuine Windows Software 3 22-03-2012 06:24 PM
Best way of observing you ads position Ur Rehman Technology & Internet 4 20-01-2011 11:35 AM
How to fix div tag at a specific position Leiff Software Development 5 04-03-2010 07:23 PM
CSS background position Gomeler Software Development 5 15-12-2009 12:54 PM
Assigning an array to an array MACE Software Development 3 18-11-2009 05:19 PM


All times are GMT +5.5. The time now is 07:10 AM.