0

I want my foreach starts of ex : $Element = 10 up to 20

At the moment :

foreach($lang['tech'] as $Element => $ElementName)
    {
       $parse['tt_name'] = $ElementName; // Displays the name

       if($Element > 99) // At over 99 foreach stop
       {
           break;  
       }
    }

I wish I had this principle in :

foreach($lang['tech'] as **$Element(10 to 20)** => $ElementName) // Display Element 10 to 20
    {
       $parse['tt_name'] = $ElementName; // Displays the name
    }

Thanks for your help

2
  • why you are not using simple for loop instead of foreach? Commented Aug 21, 2015 at 7:52
  • for ($i = 10; $i <= 20; $i++) { $parse['tt_name'] = $lang['tech'][$i]; } ? Commented Aug 21, 2015 at 7:53

7 Answers 7

3

If i understand you correct you want to loop only element 10 to 20, you could use a for loop

for($i = 10; $i <= 20;$i++) {
    $entry = $lang['tech'][$i];
    // do something
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could use continue:

foreach($lang['tech' as $Element => $ElementName)
{
   if($Element < 10) // Less than 10 - skip!
   {
       continue;  
   }

   $parse['tt_name'] = $ElementName; // Displays the name

   if($Element > 99) // At over 99 foreach stop
   {
       break;  
   }
}

Or you could use for loop:

for ($Element=10; $Element<=99; $Element++) {
    $ElementName = $lang['tech'][$Element];
}

Comments

0

you missed ] in your foreach loop

foreach($lang['tech'] as $Element => $ElementName)
                    ^

Comments

0

You're missing ] in two places in your example code:

foreach($lang['tech'] as $Element => $ElementName)
                    ^

and

foreach($lang['tech'] as **$Element(10 to 20)** => $ElementName)
                    ^

Comments

0

You can filter it before iterating:

$tech = array_filter($lang['tech'], function($key) {
    return $key >= 10 && $key <= 20;
}, ARRAY_FILTER_USE_KEY);

foreach ($tech as $key => $name) {
    // do what you need
}

1 Comment

Probably because of the PHP version :)
0
for($Element=10; $Element <=20; $Element++)
       {
       $parse['tt_name'] = $lang['tech'][$Element]; // Displays the name
    }

Comments

0

You can loop into the subarray :

foreach( array_slice ($yourarray , 10, 10) as $Element) {
...
} 

Comments

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.