Newbie array question

Newbie array question

am 22.03.2005 04:44:41 von Derek Fountain

Given this array:

array( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" )

and a string, say, "Feb", how can I get the index of that array entry? That
is, "Jan" is 0, "Feb" is 1, etc.

--
The email address used to post is a spam pit. Contact me at
http://www.derekfountain.org : href="http://www.derekfountain.org/">Derek Fountain

Re: Newbie array question

am 22.03.2005 06:38:02 von Brion Vibber

Derek Fountain wrote:
> Given this array:
>
> array( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
> "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" )
>
> and a string, say, "Feb", how can I get the index of that array entry? That
> is, "Jan" is 0, "Feb" is 1, etc.

One nice thing about PHP is that the online documentation is reasonably
searchable, and often even browseable. If you were to take a look at the
list of standard array functions:
http://www.php.net/manual/en/ref.array.php

....you'd quickly find array_search():
http://www.php.net/manual/en/function.array-search.php

-- brion vibber (brion @ pobox.com)

Re: Newbie array question

am 22.03.2005 07:07:20 von Tim Roberts

Derek Fountain wrote:

>Given this array:
>
>array( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
> "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" )
>
>and a string, say, "Feb", how can I get the index of that array entry? That
>is, "Jan" is 0, "Feb" is 1, etc.

The best way to get answers to this kind of question is to think about the
function you want. You want to search an array. Thus:

$key = array_search( "Feb", $my_array );
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: Newbie array question

am 22.03.2005 07:59:10 von Micha

array_search()

micha

Re: Newbie array question

am 22.03.2005 13:38:11 von DJ Craig

Use the array_search() function like this:
array_search($months,'Feb') will return 1.
for more info see:
http://www.php.net/manual/en/function.array-search.php

Re: Newbie array question

am 22.03.2005 14:28:01 von Danimal

Derek:

There is already a function for that:

http://us2.php.net/manual/en/function.array-search.php

-- Dannie

Re: Newbie array question

am 22.03.2005 14:30:58 von Vishal

array_search
(PHP 4 >= 4.0.5)

array_search -- Searches the array for a given value and returns the
corresponding key if successful
Description
mixed array_search ( mixed needle, array haystack [, bool strict])


Searches haystack for needle and returns the key if it is found in the
array, FALSE otherwise.

Note: If needle is a string, the comparison is done in a case-sensitive
manner.

Note: Prior to PHP 4.2.0, array_search() returns NULL on failure
instead of FALSE.

If the optional third parameter strict is set to TRUE then the
array_search() will also check the types of the needle in the haystack.


If needle is found in haystack more than once, the first matching key
is returned. To return the keys for all matching values, use
array_keys() with the optional search_value parameter instead.

Example 1. array_search() example

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>




Warning
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE, such as 0 or "". Please
read the section on Booleans for more information. Use the === operator
for testing the return value of this function.

Re: Newbie array question

am 22.03.2005 17:10:52 von BKDotCom

seriously, did you try answering this yourself?

http://www.php.net/array_search
http://www.php.net/array_keys

Re: Newbie array question

am 22.03.2005 18:14:35 von zeldorblat

$anArray = array(Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ) ;

return array_search("Feb", $anArray);

Re: Newbie array question

am 22.03.2005 18:27:38 von Ken Robinson

Derek Fountain wrote:
> Given this array:
>
> array( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
> "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" )
>
> and a string, say, "Feb", how can I get the index of that array
entry? That
> is, "Jan" is 0, "Feb" is 1, etc.

Use the array_search() function:
$mn = array( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );
echo array_search('Feb',$mn);
?>


Ken

Re: Newbie array question

am 22.03.2005 19:21:20 von DJ Craig

I already posted this a few hours ago, but it doesn't seem to have come
up. So it might get posted twice...
Use the array_search() function:

$months = array( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );
$monthnum = array_search($months, 'Feb');

This will set $monthnum to 1. For more info on this function see
http://www.php.net/manual/en/function.array-search.php .

Re: Newbie array question

am 23.03.2005 01:32:22 von Derek Fountain

> seriously, did you try answering this yourself?

Yes, I did, and I thought it was really interesting that everyone, without
exception, jumped on the same technique to solve the problem, and it wasn't
the technique I was after.

My extensive Perl experience was pointing me to the reverse hash technique,
and given that lists in PHP are implemented as arrays with default integer
indicies, I felt I was only one step from that. That was, in fact, the
question I was asking. I eventually found the correct answer myself:
array_flip() :-

$m = array_flip( array( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ) );
echo $m['Apr'];

gives 3. Interesting that everyone in a PHP group pointed me towards an
array search instead of that. Inversing the hash would have been the first
solution offered in a Perl group.

But thanks to all who responded.

--
The email address used to post is a spam pit. Contact me at
http://www.derekfountain.org : href="http://www.derekfountain.org/">Derek Fountain

Re: Newbie array question

am 24.03.2005 06:13:42 von Tim Roberts

Derek Fountain wrote:
>
>Yes, I did, and I thought it was really interesting that everyone, without
>exception, jumped on the same technique to solve the problem, and it wasn't
>the technique I was after.

We answered the question you asked, not the question you MEANT to ask.

>My extensive Perl experience was pointing me to the reverse hash technique,
>and given that lists in PHP are implemented as arrays with default integer
>indicies, I felt I was only one step from that. That was, in fact, the
>question I was asking. I eventually found the correct answer myself:
>array_flip() :-
>
>$m = array_flip( array( "Jan", "Feb", "Mar", "Apr", "May", "Jun",
> "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ) );
>echo $m['Apr'];
>
>gives 3. Interesting that everyone in a PHP group pointed me towards an
>array search instead of that. Inversing the hash would have been the first
>solution offered in a Perl group.

That depends on your need. If you are doing this once, it's foolish to
build and discard a second array solely for a single lookup. It would be
much more efficient to just call array_search.

On the other hand, if you have to do this lookup a lot, then it is
certainly smarter to build a hash.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.