3

This is only for arrays with index number. For example i have this arrays;

$array = [
  "0" => "number 1",
  "1" => "number 2",
  "2" => "number 3",
  "3" => "number 4",
  "4" => "number 5",
  "5" => "number 6",
  "6" => "number 7",
  "7" => "number 8",
  "8" => "number 9"
];

I want to skip the loop from certain range of key indexes for example, skip the foreach if the number of index is from range 0 to 5. That means we can do just like this.

foreach($array as $key => $value){
   if(array_key_exist($key, range(0,5))
      continue;
   echo $value."<br/>"
}  

or we can using for... loop

for($ind = 0; $ind < count($array); $ind++){    
    if(array_key_exist($ind, range(0,5))
      continue;    
echo $arr[$ind]."<br/>" 
}

How could i skip the index without using continue or searching the array_key first ? sure the code above looks fine to me, but if i have a bunch of arrays keys and values, i think this is not a good choice.

5
  • Why not just start your for loop at 6? for($ind = 6; $ind < count($array); $ind++){ then you don't need a check in the loop at all. Commented May 5, 2019 at 23:40
  • @Nick well that's correct, but i need to do this way, rather than start the loop at 6, beacuse this is just a basic case for bigger case, so i need to know the basic how to skipped loop for certain range of index. Commented May 5, 2019 at 23:52
  • 1
    Using array_key_exists would be pretty inefficient. It would be faster to just use if ($ind >= 0 && $ind <= 5) continue;. But... without seeing the true complexity of what you are trying to do (you mention "a bunch of arrays keys and values") it's hard to offer a good solution. Commented May 6, 2019 at 0:01
  • 1
    How about using array_diff and then loop on that output? Commented May 6, 2019 at 4:22
  • If you want to skip arbitrary range of keys {not necessary from the beginning or end of array}, best choice would be to use print_r(array_diff_key($array, array_flip(range(3,5)))); Commented May 6, 2019 at 12:26

2 Answers 2

2

You can use array_diff as:

$wantKeys = array_diff(array_keys($array), range(1,5));

Now all you need is loop on the $wantKeys as:

foreach($wantKeys as $k) 
    echo $array[$k]; // only wanted values 

The same idea can be achieve by array_diff_keys:

$wantKeys = array_diff_key($array, array_flip(range(1,5)));
Sign up to request clarification or add additional context in comments.

1 Comment

oh this is nice! but i will wait for another answer, thanks for the answer!
1

You can get the slice of array from 5th index to rest,

$result = array_slice($array,5,count($array)-5, true);

array_slice — Extract a slice of the array

Note:

array_slice() will reorder and reset the integer array indices by default. This behaviour can be changed by setting preserve_keys to TRUE. String keys are always preserved, regardless of this parameter.

Demo.

6 Comments

is there a way to make the index number not re-ordered from 0 again ?
Pardon mate. But I didn't understand, what you just say
sorry, i mean, your answer make the value number 6 and the rest have the index key re-ordered from the index 0. Is it possible to kept the old index key ?, so the value number 6 still have 5 for its key.
Yes its possible, pass true as last parameter. Check my updated answer
Your welcome mate! FYI, I updated my answer with a note of array_slice behaviour for better understanding. Thanks for the EDIT.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.