Arrays: Get the previous key.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Arrays: Get the previous key.

Post by bokehman »

Arrays: Get the previous key.

Code: Select all

$myArray = array(10 => 'something', 20 => 'something else');
Is there any way, preferably without loops, that I can feed in a number, for example 15, and the array will return the value of the first key that is equal to or less than 15?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

This is a bit long winded, perhaps someone else knows a simpler solution, or perhaps looping is a better way:

Code: Select all

<?php

function findPrevious($array, $value)
{
    $values = array_values($array);
    return $values[array_search($value, $values) -1];
}

echo findPrevious(array(10=>1, 20=>2), 2);

?>
or if you want the key:

Code: Select all

<?php

function findPreviousKey($array, $value)
{
    $values = array_values($array);
    $keys = array_keys($array);
    return $keys[array_search($value, $values) -1];
}

echo findPreviousKey(array(10=>1, 20=>2), 2);

?>
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Code: Select all

echo findPreviousKey(array(10 => 'something', 20 => 'something else'), 15);
That doesn't work!
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

That's because I believe it's made to search for exactly what it says, the previous key than the key you entered

So if you have an array like 10, 15, 20... and you enter 15, you need to modify that snippet in order for it to search for anything below 15 instead of 14 like it does now.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

m3mn0n wrote:That's because I believe it's made to search for exactly what it says, the previous key than the key you entered

So if you have an array like 10, 15, 20... and you enter 15, you need to modify that snippet in order for it to search for anything below 15 instead of 14 like it does now.
Which means a loop, doesn't it? Just what I was trying to avoid.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You can only search for existing keys, and as 15 doesn't exist in a 10 step array, it won't find it..

Try this:

Code: Select all

<?php

function findKeyLowerThan($array, $value)
{
    return max(
        array_filter(
            array_keys($array),
            create_function('$a', 'return $a < ' . $value . ';')
        )
    );
}

echo findKeyLowerThan(array(10=>1, 20=>2), 15);

?>
Post Reply