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.

 Answers

80

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

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

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
 
ariel
answered 7 Months ago
81

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->AddAddress($new_recipient_here);
    $mail->Body = <<<EOL
... customized html here
<img src="http://domain.com/email_test/email_beacon.php?campaign={$intCampaignID}&email={$address}" alt="" />
... more html here
EOL;

    $mail->send();
}
Wednesday, March 31, 2021
 
scessor
answered 7 Months ago
72

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

For example:

require(R.utils)

for(i in 1:5) {
    tryCatch(
        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
 
keyBeatz
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 :