Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:58:57+00:00 2026-06-13T09:58:57+00:00

for example I have an array : $array = array(25, 50, 75, 100); //

  • 0

for example I have an array :

$array = array(25, 50, 75, 100); // I get an array of some numbers
$position = 50; // I get some value to start with
$limit = 3; // I need to fill another array with 3 next values from the starting position

and now what code I need to use to fill a new array just like this:

$new_array = array(75, 100, 25); // the start position is 50, but I need the next 3

any ideas??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:58:58+00:00Added an answer on June 13, 2026 at 9:58 am

    I like having functions to abstract the logic. So I would suggest to write a function which takes the array, the position to start with and the limit:

    <?php
    function loop_array($array,$position,$limit) {
    
        //find the starting position...
        $key_to_start_with = array_search($position, $array);
        $results = array();
    
        //if you couldn't find the position in the array - return null
        if ($key_to_start_with === false) {
            return null;
        } else {
                //else set the index to the found key and start looping the array
            $index = $key_to_start_with;
            for($i = 0; $i<$limit; $i++) {
                        //if you're at the end, start from the beginning again
                if(!isset($array[$index])) {
                    $index = 0;
                }
                $results[] = $array[$index];
                $index++;
            }
        }
        return $results;
    }
    

    So now you can call the function with any value you want, e.g.:

    $array = array(25, 50, 75, 100);
    $position = 75;
    $limit = 3;
    
    $results = loop_array($array,$position,$limit);
    
    if($results != null) {
        print_r($results);
    } else {
        echo "The array doesn't contain '{$position}'";
    }
    

    outputs

    Array
    (
        [0] => 75
        [1] => 100
        [2] => 25
    )
    

    Or you could loop it with any other values:

    $results = loop_array(array(1,2,3,4,5), 4, 5);
    

    Here’s a working example: http://codepad.org/lji1D84J

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for example: I have created one javascript property array model with some properties like
For example i have an array like this: $test= array(0 => 412, 1 =>
For example, I have array // In $arr = [0, 1, 2, 4, 5];
Here's the problem. I ,for example,have a string 2500.Its converted from byte array into
I have the array: example = ['foo', 'bar', 'quux'] I want to iterate over
I have an array of strings(as shown in example). I just wish to find
I have an array, for example (in Java) int[] a = new int[N]; I
I have this array, for example (the size is variable): x = [1.111, 1.122,
I have an array of 4 characters, for example: char char_array[10]; char_arr[0] = 'a';
I have an array Y of the form(it is just an example, i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.