3

I want to be able to retrieve the value of an array by using the numeric key. The catch is that if the key is beyond the array length, I need it to loop through the array again.

$my_array = array('zero','one','two','three','four','five','six','seven');
function loopArrayValues($array,$key){
    //this is what is needed to return
    return 
}
echo "Key 2 is ".loopArrayValues($my_array,2)."<br />";
echo "Key 11 is ".loopArrayValues($my_array,11)."<br />";
echo "Key 150 is ".loopArrayValues($my_array,11)."<br />";

Expected output:

Key 2 is two
Key 11 is three
Key 150 is three

My research references:

My formed function:

function loopArrayValues($array,$key){
  $infinate = new InfiniteIterator(new ArrayIterator($array));
  foreach( new LimitIterator($infinate,1,$key) as $value){
    $return=$value;
  }
  return $return;
}

The function works, but I have a question: is this a good way to get the intended results?

Community
  • 1
  • 1
amaster
  • 1,915
  • 5
  • 25
  • 51
  • 2
    Do you mean that if the key drops off the end of the array, it should wrap around again? So, the end plus one is the beginning? If so, something like `$my_array[ $key % count( $my_array ) ]`? – halfer May 17 '13 at 16:33
  • @billyonecan fixed, sorry copy pasted code and not array. First value in array is 'zero' – amaster May 17 '13 at 16:34
  • @amaster507 no need to apologise, I was just making sure I hadn't missed something. I still don't understand why, if the key passed is greater than the array length, it'd return three. Could you elaborate on the logic? – billyonecan May 17 '13 at 16:36
  • @billyonecan for instance if I try to ` echo $my_array[11];` under normal circumstances it will return `Notice: Undefined offset: 11 in [...][...] on line 20
    ` when instead I would want it to return `six` I am trying to use this inside a FPDF function to display a color for a text with a variable color scales, sometimes there might be one color and sometimes there might be 3 colors. First line gets color 1, second line gets color two, etc. and without breaking.
    – amaster May 17 '13 at 16:47
  • @amaster507: see my comment above, or vascowhite's answer below. – halfer May 17 '13 at 16:48

1 Answers1

7

You are being far too complex, unless you actually want to process the elements in the array you don't want to iterate over them as it is expensive. I think you just need the modulus of the number of elements in the array, like this:-

$my_array = array('zero', 'one','two','three','four','five','six','seven');

function loopArrayValues(array $array, $position)
{
    return $array[$position % count($array)];
}

for($i = 0; $i <= 100; $i++){
    echo "Position $i is " . loopArrayValues($my_array, $i) . "<br/>";
}

Ouput:-

Position 0 is zero
Position 1 is one
Position 2 is two
Position 3 is three
Position 4 is four
Position 5 is five
Position 6 is six
Position 7 is seven
Position 8 is zero
Position 9 is one
Position 10 is two
Position 11 is three
Position 12 is four
Position 13 is five

etc...

vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • @halfer and @ vascowhite and Thanks! i am always trying to overcomplicate the matter. – amaster May 17 '13 at 16:50
  • This works well if you loop off the end of the array, but not if you loop off the beginning. Which I know the OP wasn't asking about, but others might be interested. – Chris Rae Jan 11 '17 at 23:28