Asked  7 Months ago    Answers:  5   Viewed   36 times

I have a foreach loop that i need to limit to the first 10 items then break out of it.

How would i do that here?

foreach ($butters->users->user as $user) {
    $id = $user->id;
    $name = $user->screen_name;
    $profimg = $user->profile_image_url;
    echo "things";    

Would appreciate a detailed explanation as well.



If you want to use foreach, you can add an additional variable to control the number of iterations. For example:

foreach ($butters->users->user as $user) {
    if($i==10) break;
    $id = $user->id;
    $name = $user->screen_name;
    $profimg = $user->profile_image_url;
    echo "things";  
Wednesday, March 31, 2021
answered 7 Months ago
if (strlen($str) > 100) $str = substr($str, 0, 100) . "...";
Wednesday, March 31, 2021
answered 7 Months ago

In PHP 7, if you iterate an array by value, the copy will be done lazily, only when and if the array is actually modified.

If you iterate an array by reference instead, a separation will be performed at the start of the loop. If the array is currently used in more than one place, this separation will lead to a copy.

Furthermore iterating by reference means that a) the array has to be wrapped into a reference and b) each element has to be wrapped in a reference as well. Creating a reference wrapper is an expensive operation, because it requires allocation.

Additionally iteration by reference requires us to use a modification-safe iteration mechanism. This works by registering the iterator with the array and checking for potentially affected iterators in various array modification operations.

So no, iterating by reference is certainly not an optimization, it's a de-optimization. Using references usually is.

Wednesday, March 31, 2021
answered 7 Months ago

Instead of using BCC, you'd do something like:

... initialize PHP mailer ...
... set options common to ALL emails ...
foreach( ... loop over all recipients ...) {
    $mail->ClearAddresses(); // remove previous email addresses
    $mail->Body = <<<EOL
... customized html here
<img src="{$intCampaignID}&email={$address}" alt="" />
... more html here

Wednesday, March 31, 2021
answered 7 Months ago

withTimeout() from package R.utils, in concert with tryCatch(), might provide a cleaner solution.

For example:


for(i in 1:5) {
        expr = {
            withTimeout({Sys.sleep(i); cat(i, "n")}, 
                         timeout = 3.1)
        TimeoutException = function(ex) cat("Timeout. Skipping.n")

# 1 
# 2 
# 3 
# Timeout. Skipping.
# Timeout. Skipping.

In the artificial example above:

  • The first argument to withTimeout() contains the code to be evaluated within each loop.

  • The timeout argument to withTimeout() sets the time limit in seconds.

  • The TimeoutException argument to tryCatch() takes a function that is to be executed when an iteration of the loop is timed out.

Thursday, July 29, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :