Viewed   78 times

We have all heard of how in a for loop, we should do this:

for ($i = 0, $count = count($array); $i < $c; ++$i)
{
    // Do stuff while traversing array
}

instead of this:

for ($i = 0; $i < count($array); ++$i)
{
    // Do stuff while traversing array
}

for performance reasons (i.e. initializing $count would've called count() only once, instead of calling count() with every conditional check).

Does it then also make a difference if, in a foreach loop, I do this:

$array = do_something_that_returns_an_array();

foreach ($array as $key => $val)
{
    // Do stuff while traversing array
}

instead of this:

foreach (do_something_that_returns_an_array() as $key => $val)
{
    // Do stuff while traversing array
}

assuming circumstances allow me to use either? That is, does PHP call the function only once in both cases, or is it like for where the second case would call the function again and again?

 Answers

5

foreach() is implemented using iterators - thus it only calls the function that returns an array once, and then uses the iterator which points to the existing result set to continue with each item.

Saturday, October 22, 2022
2

Take note of the 'this' Context

When an event fires in jquery the callback function this context is set to the element which the event was fired from.

jQuery(document).ready(function($) {
    $(".input_img").click(function() {
        // Use `this` to target the event element
        $(this).attr("src", "<?php the_field('acf_numero_image', $related_post->ID); ?>" );
    });
});

Advice: You shouldn't generate same jquery code inside each iteration of the for each. Since you're repeating unnecessary code. You can harness HTML data-* attributes to achieve the outcome you seek.

Thursday, August 11, 2022
 
3
<?php
  for ($n = 0; $n <= 7; $n++) {
    echo '<p>'.($n + 1).'</p>';
    echo '<p>'.($n * 2 + 1).'</p>';
  }
?>

First paragraph:

1, 2, 3, 4, 5, 6, 7, 8

Second paragraph:

1, 3, 5, 7, 9, 11, 13, 15
Tuesday, September 27, 2022
 
lyd
 
lyd
5

First of all initialize the variable you want to use later on:

$checkmissing = array();

Then inside the foreach append the first entry of the post terms to that array:

foreach($gallids as $gallterm)
{
    list($checkmissing[]) = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"));
}

See $checkmissing[], that is what effectively will prevent that you overwrite it. It will append each to the array.

Finally you can output the result after the loop:

print_r($checkmissing);

Note: You should do some additional handling if wp_get_post_terms returns an empty array:

foreach($gallids as $gallterm)
{
    $terms = wp_get_post_terms($gallterm, 'type', array("fields" => "slugs"))
        AND list($checkmissing[]) = $terms
    ;
}
Tuesday, December 20, 2022
 
4

You don't want individual variables, try an array:

foreach($results as $result){
    $names[] = $result->username;
}

In PHP 7 it's even easier:

$names = array_column($results, null, 'username');
Wednesday, October 5, 2022
 
291086
 
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 :