How to expand an array_shift beyond the amount of indexes?

Asked

Viewed 32 times

0

I have a question. I put the code just below before starting a while but inside the loop, calling the array_shift($cores), it applies the classes while loop is <= the number of indices, that is to say índice 4 he no longer applies the classes.

If I then put 10 items inside the loop, 6 will be without any class probably because this instruction puts index without repeating it.

$cores = ["primary", "secondary", "tertiary", "quaternary"];
shuffle($cores);

LOOP

if ( $empr->have_posts() ) {
    while ( $empr->have_posts() ) { $empr->the_post(); ?>
        <div class="team-item <?php echo array_shift($cores); ?>"> ...

Question: How to continue applying classes? An example can be viewed here in the session OTHER VENTURES.

  • Maybe use the rand() and play the return as index of the array can be a solution, so no array element is removed.

2 answers

3


Using an array_shift

You can rotate the array thus:

array_push( $cores, array_shift($cores) );

Applied to the code:

if ( $empr->have_posts() ) {
    while ( $empr->have_posts() ) {
        $empr->the_post();
        echo '<div class="team-item ' . $cores[0] . '">';
        array_push( $cores, array_shift($cores) );
        ...

Explanation:

When you give a array_shift in { 1, 2, 3, 4 }, the function will return 1, and at the same time the array will turn { 2, 3, 4 }.

Then we give a array_push, catching the 1 returned, and adding to the end of it array. Like the array had been shortened to { 2, 3, 4 }, by adding the 1 in the end he turns { 2, 3, 4, 1 }, and so on: { 3, 4, 1, 2 }, { 4, 1, 2, 3 }, { 1, 2, 3, 4 } ...


Alternative with module

It follows an alternative that I find even leaner than with array_shift.

$i = 0;
if ( $empr->have_posts() ) {
    while ( $empr->have_posts() ) { $empr->the_post(); ?>
        <div class="team-item <?php echo $cores[$i++ % 4]; ?>"> ...
  • Good hint, I believe your answer lacks the Dice of the array_push array($colors[0],array_shift($colors)), but checks the usage with pop also that is better.

  • 1

    @Guerra push and pop will work on the same "tip" of the array. Shift returns only one item, and at the same time removes it from the array. then at push time the array will already be shorter, and be added to the end. If you put index, you will get 2 items only. I will update the answer explaining this.

  • Well said, but from what I saw the pop to perform at the end of the array does not need to update the index of all occurrences.

  • 2

    @Guerra I only answered with array_shift pq was in question, but I think the 2nd solution I put is a bit more technical than the 1st, to tell you the truth. It’s just that I also like the idea of explaining the functions, to help people learn their inner workings, to use for other things as well.

2

I don’t know if I got it right, but array_shift removes the right first item?

I imagine something like that will solve your problem:

$cores = ["primary", "secondary", "tertiary", "quaternary"];
shuffle($cores);
$tmp = null;

if ( $empr->have_posts() ) {
while ( $empr->have_posts() ) { $empr->the_post(); ?>
    $tmp = is_null($tmp) ? $cores : $tmp;
    <div class="team-item <?php echo array_shift($tmp); ?>"> ...

I studied it here, and I believe that the pop array_pop is more indicated because it does not need to update the array’s Dice. It would look like this (I changed the suit too to see if it goes):

$cores = ["primary", "secondary", "tertiary", "quaternary"];
shuffle($cores);
$tmp = null;

if ( $empr->have_posts() ) {
while ( $empr->have_posts() ) { $empr->the_post(); ?>
    $tmp = !$tmp ? $cores : $tmp;
    <div class="team-item <?php echo array_pop($tmp); ?>"> ..

The array_pop does the same thing as shift but in the last element of the array, as you do not question the order I imagine will serve the same, but with improved performace.

  • What tin @War Still Not Returning Classes.

  • 1

    I updated the answer with a better one, make sure you’re using $tmp in echo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.