我怎样才能在php中取消for和foreach循环值

时间:2016-03-24 05:01:11

标签: php for-loop foreach

我的HTML代码是:

<table cellpadding="0" cellspacing="0" border="1" class="pro_code_table">
    <tr>
        <th colspan="12" class="pro_code_lable_with_data">Product Code Detailes</th>
    </tr>
    <tr>
        <td class="table_val_data">Title</td>
    @foreach($x as $k_x => $v_x)
        <td class="table_val_data">{{ $v_x }}</td>  
    @endforeach
    </tr>
    <tr>
    @foreach ($y as $k_y => $v_y) 
        <td class="table_val_data">{{ $v_y }}</td>
    <?php $pro_code_count=count($pro_code);
    //print_r($pro_code);
    ?>
    @for($r = 1; $r <=sizeof($x); $r++)
        <td>
        <?php $i=1; ?>
        @foreach($pro_code as $key=>$val)
        @if($i % 2 != 0)
          <input type="text" name="pro_code[]" value="{{ $val }}">
        @else
          <input type="text" name="pro_code[]" value="{{ $val }}">
        @endif
        @endforeach
        <?php 
        $i++;
        ?>
        </td>
        @endfor
    </tr>
    @endforeach
</table>

因为我可以在表格中循环数据,X和Y轴正确循环,但在<td>数据中循环多次,

如何停止循环过程

我的o \ p看起来如下

enter image description here

我想要像下面这样的数据

enter image description here

1 个答案:

答案 0 :(得分:0)

<table cellpadding="0" cellspacing="0" border="1" class="pro_code_table">
<tr>
<th colspan="12" class="pro_code_lable_with_data">Product Code Detailes</th>
</tr>
<tr>
<td class="table_val_data">Title</td>
@foreach($x as $k_x => $v_x)
<td class="table_val_data">{{ $v_x }}</td>
@endforeach            
</tr>

<tr>
@foreach($y as $k_y_t=> $v_y_t)
<td class="table_val_data">{{ $v_y_t }}</td>

@foreach($y as $k_y=> $v_y)
    @foreach($x as $k_x => $v_x)
    @if($k_y_t==$k_y)
        <td class="table_val_data">
    @foreach($pro_code as $k_pro_code=> $v_pro_code)            
    <?php
    if($k_x.'-'.$k_y==$k_pro_code){
        echo '<input type="text" name="value" value="'.$v_pro_code.'">';
    }                   
    ?>
    @endforeach             
    </td>
    @endif    
    @endforeach 
@endforeach 
</tr>

@endforeach
</table>

$x: {"3":"bule","4":"green"}
$y: {"5":"cotten","6":"polyester"}
$pro_code_in_edit: {"3-5":"12","4-5":"23","3-6":"56","4-6":"89"}

enter image description here