John Doe
John Doe

Reputation: 158

Basic PHP Array Iteration Issue

I hate to be one of "those guys" - I'm very sure the answer is right in front of my face and I simply can't grasp it. However I have tried for the last few hours to figure this out with the help of our friend Mr. Google to no avail.

What I am trying to do is quite simple:

  1. Push a string to an array.
  2. Count through the entries in the array X number of times with an offset, and return the resulting value as a variable.

For all my labor, I have just this:

$newarray = array(1,2,3,4,7,5,8,6,9);

I'd like to be able to count (for example) starting at the number 3, increment by a variable number (for example, 12) in something of a round robin manner. This would put the end of the count at the number 5. I'd then like to take that number (5) and store it in a variable for use on the rest of the page.

Again, I know this should be elementary but for whatever reason I just can't seem to figure it out. Thank you very much for your help.

Upvotes: 3

Views: 107

Answers (3)

Neil Richins
Neil Richins

Reputation: 91

1) Push a string to an array can be done with array_push.

2) Counting through the entries in the array X number of times with an offset, and return the resulting value as a variable is a little more complicated. We can use count() and add 1 to get the number of elements in an array and compute the ordinal key offset using the PHP modulus operator % If the array is not already keyed with ordinal values, array_value() will rekey the array with ordinal values for us. Then we just return the value.

Below is a small bit of code that pulls it all together for you.

    <?php
    $myArray = array(1,2,3,4,7,5,8,6);
    array_push($myArray,'9'); 
    $start=3;
    $increment=12;

    echo roundRobbinArrayValue($myArray,$start+$increment)."\n";

    function  roundRobbinArrayValue($myArray,$pointer){
        $ordinalArray=array_values($myArray);
        $numberOfItems=count($ordinalArray)+1; 
        $newPointer= $pointer % $numberOfItems;
        return $ordinalArray[$newPointer];      
    }  
    ?>

Upvotes: 0

Sari Rahal
Sari Rahal

Reputation: 1955

For your number 1 you will want to use array_push() : http://php.net/manual/en/function.array-push.php .

Here is an example:

<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
?>

This will display

Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)

For your number 2. You can use a simple for loop like this:

for($i = 0; $i< $stack.count(); $i++){
    $tmp = $stack[$i];
    //tmp will be the value at that location do what you will with it
}

For the offset, just set $i to your offset

for($i = $offset; $i < $stack.count(); $i++){
    $tmp = $stack[$i];
    //tmp will be the value at that location do what you will with it
}

Upvotes: 0

Thamilhan
Thamilhan

Reputation: 13313

Here is one of the way:

  1. Changing the position of the desired element to first position
  2. Finally, pick the element which you desire after a circulating through the array.

PHP Code:

<?php
function circulateArr($key, $arr)
{
    foreach($arr as $arrkey => $value)
    {
        if($arrkey != $key)
        {
            $elm = array_shift($arr);
            array_push($arr, $elm);
        }
        else
        {
            break;
        }
    }
    return $arr;
}


$array = array(1,2,3,4,5,6,7,8,9);
$start = 3;
$roundRobin = 12;

$arr = circulateArr(array_search($start, $array), $array); //Repositioning the array

echo $elementChose = $arr[($roundRobin%count($arr))-1]; //Get the array element

Output:

5

Upvotes: 2

Related Questions