Viewed   83 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

2

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++;  
} 
Sunday, October 16, 2022
 
elbert
 
4

You're looking for the array_slice function:

array_slice() returns the sequence of elements from the array array as specified by the offset...

$lines = file(...);
$lines = array_slice($lines, -50);
Thursday, October 13, 2022
 
alan
 
1

You should open and close your <rows/> in the books loop, and add a late check for odd books:

<?php 
$count = 0;
foreach ($contents as $content) 
{
    //var_dump($content);
    $books      = $content["tags"];
    $book_image = $content['content_image'];
    $book_desc  = $content['content_social_description'];


    foreach ($books as $book) 
    {
        ++$count;
        if($count == 1)
        {  
            echo "<div class='et_pb_row'>";
        }
        $book_name      = $book['tag_name'];
        $book_name_trim = str_replace(' ', '-', $book_name);
        ?>
        <!-- Inside the Book Loop -->
        <div class='et_pb_column et_pb_column_1_2 books' style="background: url('https://s3-us-west-2.amazonaws.com/crowdhubproverbs31/<?php echo $book_image ;?>');">
            <h2><?php echo $book_name; ?></h2>
            <p><?php echo $book_desc; ?></p>
            <?php echo $count; ?>
        </div>

        <?php
        if ($count == 2)
        {
            echo "</div>";
            $count = 0;
        }


    }
}

if ($count > 0)
{
    echo "</div>";
}
?>

Doing so, your $count variable will be incremented only when foreach($books AS $book) loop is run (thus you have at least one book to print)

Thursday, October 20, 2022
 
4

So after researching a bit more @diggy's idea with pre processing the_content and adding the $atts to the shorcodes before the shortcodes are rendered, I came up with this:

function my_shortcode_content( $content ) {
 //check to see if the post has your shortcode, skip do nothing if not found
 if( has_shortcode( $content, 'shortcode' ) ):

  //replace any shortcodes to the basic form ([shortcode 1] back to [shortcode])
  //usefull when the post is edited and more shortcodes are added
  $content = preg_replace('/shortcode .]/', 'shortcode]', $content);

  //loop the content and replace the basic shortcodes with the incremented value
  $content = preg_replace_callback('/shortocode]/', 'rep_count', $content);

 endif;

return $content;
}
add_filter( 'content_save_pre' , 'my_shortcode_content' , 10, 1);

function rep_count($matches) {
 static $i = 1;
return 'shortcode ' . $i++ .']';
}
Tuesday, December 20, 2022
 
1

No, you can't calculate this, it varies too much from browser to browser. Some browsers do it by time (e.g., your code has run for more than X seconds without yielding back to the browser; on Firefox 7 the default is 10 seconds but the user can change it), other browsers do it by number of operations (IE, for instance).

Your best bet is to refactor your code so you avoid even beginning to approach the limit. If you have a large piece of work to do, break it into chunks, then run the chunks one after another using setTimeout with a timeout value of 0. This yields back to the browser (for more than 0 milliseconds; exactly how long will vary by browser and by what other things are going on on the page).

Here's an example of counting to 10,000,000 in 100,000 iteration chunks:

function toAMillion() {
  var counter = 0,
      limit = 10000000,
      chunk = 100000;

  doAChunk();

  function doAChunk() {
    var target = Math.min(limit, counter + chunk);

    while (counter < target) {
      ++counter;
    }

    if (counter < limit) {
      // Schedule next chunk
      setTimeout(doAChunk, 0);
    }
    else {
      // Done
      display("Done, counter = " + counter);
    }
  }
}

Live copy

Saturday, December 17, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :