How to iterate within Magic Fields Group foreach loop? [closed]

Update #2:
Use my first option to get all of the IDs in an array and then loop through those. I keep trying to understand how / what exactly you are trying to achieve (use the same variable to hold an ID and somehow perform an asynchronous operation below the first block of code which is for “group one”?!) and I can only come to the conclusion you have some deep misunderstandings about how PHP works.

However, I can understand that you want all of the IDs and then iterate through them in which case my first proposed option is exactly what you need.

$post_ids = array();

foreach ( $chall_entries as $art_entry ): //artisan challenges only
    if ( $art_entry['challenge_entry_challenge_type'][1] == 'Artisan' ):
        // I have NO IDEA what you mean about: 
        // "Because then Group Loop 1 will repeat for each Group Loop 1 and I don't want that to happen"
        $post_ids[] = (int)$art_entry['challenge_entry_assign_task'][1];        
    endif;
endforeach;

foreach ( $post_ids as $post_id ):
    $artisan_chall_task = get_group( 'task_entry', $post_id ); // Passing the $post_id

    foreach ( $artisan_chall_task as $art_chall_task ):
        $task_ach_id = intval($art_chall_task['task_entry_achievement_id'][1]); //achievement ID
        $task_ach_link = get_the_permalink($task_ach_id); //achievement link
        echo "<tr>";  
        echo "<td class="mobile-first">" . $art_chall_task['task_entry_task_name'][1] . "</td>";
        echo "<td class="mobile-second">" . $art_chall_task['task_entry_task_desc'][1] . "</td>";
        echo "<td class="mobile-third"><div class="" . $art_chall_task["task_entry_task_difficulty'][1] . "-stars margin-auto'></div></td>";
        echo "<td class="mobile-fourth"><a href="" . $task_ach_link . "">" . get_the_post_thumbnail( $task_ach_id, 'thumbnail' ) . "</a></td>";
        echo "</tr>"; 
    endforeach;

endforeach;

Update #1:

By the looks of the posted code it seems that you are basically overwriting the global variable when going through the foreach loop instead of executing the code from the “second group” block after each pass. As I see it you have two options: to either declare an array before the “first group” code, something like $post_ids = array() and push each ID to the array in the “first group” code:

$post_ids[] = intval($art_entry['challenge_entry_assign_task'][1]);

After that is done you can then proceed with something like this:

foreach ( $post_ids as $post_id ):

    $artisan_chall_task = get_group( 'task_entry', $post_id ); // Passing the $post_id

    foreach($artisan_chall_task as $art_chall_task):
        $task_ach_id = intval($art_chall_task['task_entry_achievement_id'][1]); //achievement ID
        $task_ach_link = get_the_permalink($task_ach_id); //achievement link
        echo "<tr>";  
        echo "<td class="mobile-first">" . $art_chall_task['task_entry_task_name'][1] . "</td>";
        echo "<td class="mobile-second">" . $art_chall_task['task_entry_task_desc'][1] . "</td>";
        echo "<td class="mobile-third"><div class="" . $art_chall_task["task_entry_task_difficulty'][1] . "-stars margin-auto'></div></td>";
        echo "<td class="mobile-fourth"><a href="" . $task_ach_link . "">" . get_the_post_thumbnail( $task_ach_id, 'thumbnail' ) . "</a></td>";
        echo "</tr>"; 
    endforeach;

endforeach;

The other option would be to wrap the “second group” code in a function that accepts a parameter which is the ID of a post:

function print_task_entry( $post_id ) {
    $artisan_chall_task = get_group( 'task_entry', $post_id ); // Passing the $post_id

    foreach($artisan_chall_task as $art_chall_task):
        $task_ach_id = intval($art_chall_task['task_entry_achievement_id'][1]); //achievement ID
        $task_ach_link = get_the_permalink($task_ach_id); //achievement link
        echo "<tr>";  
        echo "<td class="mobile-first">" . $art_chall_task['task_entry_task_name'][1] . "</td>";
        echo "<td class="mobile-second">" . $art_chall_task['task_entry_task_desc'][1] . "</td>";
        echo "<td class="mobile-third"><div class="" . $art_chall_task["task_entry_task_difficulty'][1] . "-stars margin-auto'></div></td>";
        echo "<td class="mobile-fourth"><a href="" . $task_ach_link . "">" . get_the_post_thumbnail( $task_ach_id, 'thumbnail' ) . "</a></td>";
        echo "</tr>"; 
    endforeach;    
}

And the code for your “first group” would look like this

$chall_entries = get_group('challenge_entry', $post_id = 4643);
foreach($chall_entries as $art_entry): //artisan challenges only
    if( $art_entry['challenge_entry_challenge_type'][1] == 'Artisan' ):
        // No need for any global variable!
        $task_post_id = intval($art_entry['challenge_entry_assign_task'][1]);
        print_task_entry( $task_post_id );
    endif;
endforeach;

Original:

There are several potential issues I see with your code:

$art_entry['challenge_entry_challenge_type'][1] == 'Artisan'

Assuming you wanted the first element in the art_entry['challenge_entry_challenge_type'] array then that line should be:

$art_entry['challenge_entry_challenge_type'][0] == 'Artisan'

As you can see, the first element in an array is at index 0.

Second, don’t use the global keyword: global $task_post_id;. You can in fact delete that line all together. Declaring that variable as global can lead to unpredictable behaviour unless you definitely know what you are doing. Global means that if that same combination of global and name variable is used somewhere else in the code it can be overwritten and your value could not be what you expected.

Third, the intval call can have a significant impact on your code performance during a loop so avoid that. If you really need to make sure that value is an integer you can typecast it with (int). So that line should look something like this:

$task_post_id = (int)$art_entry['challenge_entry_assign_task'][1];

Finally, it is not entirely clear to me if all of the IDs you are looking for are in this array variable: $art_entry['challenge_entry_assign_task']

If they are and are located at indexes such as $art_entry['challenge_entry_assign_task'][0], $art_entry['challenge_entry_assign_task'][1], $art_entry['challenge_entry_assign_task'][2] etc than you definitely need a looping mechanism if you want to access incrementally the value at a particular index.

I can see from a comment you’ve added that you do want to iterate through that array: [...] I don't want to manually write [1], [2], [3] etc. How do I iterate through that dynamically? – RachieVee 22 hours ago

In this case a classic for loop mechanism can be used:

$length = count( $art_entry['challenge_entry_assign_task'] ); // Get number of elements in array

for ( $i = 0; $i < $length; $i++ ) {
    $task_post_id = (int)$art_entry['challenge_entry_assign_task'][$i]; // Accesing the element at index $i
    echo $task_post_id;
}

I hope this helps in clearing up the situation.