[UPHPU] better way to find position in an array of numeric
values?
jim
thinbegin at gmail.com
Sat May 2 16:52:39 MDT 2009
It seems like the array_search() function was designed for just this
sort of scenario [if i am understanding your scenario correctly].
array_search() looks for an array value and, if found, returns that value's key.
On Sat, May 2, 2009 at 7:57 AM, Wade Preston Shearer
<wadeshearer.lists at me.com> wrote:
> I have an array called $things that I am iterating through. On each loop I
> need to echo out some values of the sub-array. I also need to echo out a
> "position." The position is a text string representation of a numeric value.
> I have an array of possible positions. The correct position is the one
> immediately less than the ID. So, if the ID was 32, then the position would
> be 'b'. Is there a more efficient way to determine and return the position
> than how I am doing it below?
>
>
> function position($id) {
> $positions = array(10 => 'a', 22 => 'b', 46 => 'c', 88 => 'd');
>
> foreach($position as $key => $value) {
> if($id < $key) {
> $position = $value;
> }
> }
>
> return $position;
> }
>
> foreach($things as $value) {
> echo "\n" . $value['name'] . ' is in position ' .
> position($value['id']) . '.';
> }
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
>
More information about the UPHPU
mailing list