Solved

get the position of an array element

Posted on 2007-11-18
4
4,018 Views
Last Modified: 2012-05-05
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");

Open in new window

0
Comment
Question by:bdipasquale
4 Comments
 
LVL 9

Accepted Solution

by:
blue_hunter earned 250 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

by:nizsmo
nizsmo earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now