Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# get the position of an array element

Posted on 2007-11-18
Medium Priority
4,045 Views
below ic a copy of my array, I would like to know how to get the position of a particular element

eg  get the position of 1998
``````\$yearArray = array("1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009");
``````
0
Question by:bdipasquale

LVL 9

Accepted Solution

blue_hunter earned 1000 total points
ID: 20310237
\$yearArray = array("1994","1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009");

\$searchElement = "1998";
\$position = array_search(\$searchElement, \$yearArray); // \$position = 4;

* position start at 0
0

LVL 21

Assisted Solution

nizsmo earned 1000 total points
ID: 20310460
alternatively you can define the key yourself like this:

\$yearArray = array("year1" => "1994", "year2" => "1995", "year3" => "1996", "year4" => "1997", "year5" => "1998", "year6" => "1999", "year7" => "2000", "year8" => "2001", "year9" => "2002", "year10" => "2003", "year11" => "2004", "year12" => "2005", "year13" => "2006", "year14" => "2007", "year15" => "2008", "year16" => "2009");

so that you know exactly which element has which year, you can change the keys accordingly i did the above only as an example as an alternative solution.

To access year 2000:
echo \$yearArray["year7"];

it all depends what you want to use the array for essentially, both methods have very different uses.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
###### Suggested Courses
Course of the Month11 days, 2 hours left to enroll