Asked  7 Months ago    Answers:  5   Viewed   533 times

I am looking for a way to get the next and next+1 key/value pair in a foreach(). For example:

$a = array('leg1'=>'LA', 'leg2'=>'NY', 'leg3'=>'NY', 'leg4'=>'FL');

foreach($a AS $k => $v){

    if($nextval == $v && $nextnextval == $v){
       //staying put for next two legs
    }

}

 Answers

90

You can't access that way the next and next-next values.

But you can do something similar:

$a = array('leg1'=>'LA', 'leg2'=>'NY', 'leg3'=>'NY', 'leg4'=>'FL');

$keys = array_keys($a);
foreach(array_keys($keys) AS $k ){
    $this_value = $a[$keys[$k]];
    $nextval = $a[$keys[$k+1]];
    $nextnextval = $a[$keys[$k+2]];

    if($nextval == $this_value && $nextnextval == $this_value){
       //staying put for next two legs
    }
}
Wednesday, March 31, 2021
 
antoniputra
answered 7 Months ago
32

Move the table tags inside the IF statements

<?php foreach($credits as $credit) : ?>
    <?php if($credit['credit_type'] == "short") : ?>
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td><?php echo $credit['category_position']; ?></td>
            <td><?php echo $credit['category_title']; ?></td>
        </tr>
        <tr>
            <td><?php echo $credit['credit_heading']; ?></td>
            <td><a href="">Edit</a></td>
        </tr>
      </table>
    <?php endif; ?>
    <?php if($credit['credit_type'] == "long") : ?>
       <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td><?php echo $credit['category_position']; ?></td>
            <td><?php echo $credit['category_title']; ?></td>
            <td><strong>Title</strong></td>
            <td><strong>Role</strong></td>
            <td><strong>Director</strong></td>
        </tr>
        <tr>
            <td><?php echo $credit['credit_position']; ?></td>
            <td><?php echo $credit['credit_heading']; ?></td>
            <td><?php echo $credit['credit_title']; ?></td>
            <td><?php echo $credit['credit_role']; ?></td>
            <td><?php echo $credit['credit_director']; ?></td>
        </tr>
       </table>
    <?php endif; ?>
<?php endforeach; ?>

Note this is only going to work if you only ever have 2 credit_type values

Wednesday, March 31, 2021
 
weegee
answered 7 Months ago
16

Maybe this works for you:

{foreach from=$slider key=i item=sliderr}
    <div id="sliderBox">
<div id="showcase" class="showcase">
        <div class="showcase-slide">
            <div class="showcase-content">
{$sliderr.img}
            </div>
        </div>
        <div class="showcase-slide">
            <div class="showcase-content">
                {$slider[{$i+1}]}
            </div>
        </div>  
</div>
</div>
{/foreach}
Saturday, May 29, 2021
 
muffe
answered 5 Months ago
34

A unique approach would be to reverse the array and then loop. This will work for non-numerically indexed arrays as well:

$items = array(
    'one'   => 'two',
    'two'   => 'two',
    'three' => 'three'
);
$backwards = array_reverse($items);
$last_item = NULL;

foreach ($backwards as $current_item) {
    if ($last_item === $current_item) {
        // they match
    }
    $last_item = $current_item;
}

If you are still interested in using the current and next functions, you could do this:

$items = array('two', 'two', 'three');
$length = count($items);
for($i = 0; $i < $length - 1; ++$i) {
    if (current($items) === next($items)) {
        // they match
    }
}

#2 is probably the best solution. Note, $i < $length - 1; will stop the loop after comparing the last two items in the array. I put this in the loop to be explicit with the example. You should probably just calculate $length = count($items) - 1;

Tuesday, June 29, 2021
 
jenny
answered 4 Months ago
25

Do you mean something like:

foreach($_POST['something'] as $key => $something) { 
    $example = $_POST['example'][$key];
    $query = mysql_query("INSERT INTO table (row, row2) VALUES ('{$something}','{$example}')"); 
} 
Wednesday, August 11, 2021
 
PeanutsMcgee
answered 2 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 :