开发者

Get next from array in a loop

i am so stuck , please someone help if you have 2 min.

i have a foreach where I get values from session something like

foreach ($result as $i => $item ) {

         $values_array  = explode("\n",$get_widths); // outputs Array ( [0] => 120 [1] => 180) 
         // here I need to echo the value from the array but only ONCE
}

the issue is that this is used by 2 or more items and array outputs gets repeated every time , so if 2 items i get Array ( [0] => 120 [1] => 180) Array ( [0] => 120 开发者_如何学Python[1] => 180) or 120 120 instead

120 180

for actual code see here

<?php
foreach ($list as $i => &$item){
    $i=0;
    $is_group                   = $item_params->get('is_group');    
    $group_widths               = $item_params->get('group_widths');    
    $group_widths_array         = explode("\n",$group_widths);

    if($is_group == 1){

        echo $group_widths_array[$i];
    }



}
?>

please note I cant move outside the foreach. thank you


I would simply use a flag variable of some sort to determine if we have processed yet or not.

$values_array  = explode("\n",$get_widths);
if (!isset($we_have_outputted)) {
   echo '<pre>'. print_r($values_array, true) .'</pre>';
   $we_have_outputted = true;
}


I am completely missing the point of this loop...

You are iterating over an array, assigning the key to $i and the value to $item but then you set $i to 0, echoing $group_widths_array[0] every time your condition is met and $item you don´t use at all...

That can´t be right, can it?

Anyway, it seems to me that the reason that your output is repeated is that you output $group_widths_array[0] every time and $is_group is the same on every iteration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜