如何在php

时间:2016-03-30 05:04:08

标签: php arrays loops for-loop foreach

这是我的代码:

 foreach ($category_attribute_list_value as $key => $value) {
        foreach ($value as $key2 => $value_fial) {
            if($key==0){
                $x[]=$value_fial->attribute_value;
                $x_id[]=$value_fial->attribute_value_id;
            }
        }}
        foreach ($category_attribute_list_value as $key_y => $value_y) {
        foreach ($value_y as $key2_y => $value_fial_y) {
            if($key_y!=0)
                {
                $y[]=$value_fial_y->attribute_value;
                $y_id[]=$value_fial_y->attribute_value_id;
                }
        }}

在上面的代码中,$ x包含两个值(L,XL),对于$ y,它们相同,它有两个值(蓝色,绿色)。

现在来到表数据代码

我的表格代码是:

echo'<table cellpadding="0" cellspacing="0" border="1" class="pro_code_table">
<tr>
<td class="table_val_data">Code</td>';
foreach($x as $k_x => $v_x){
echo'<td class="table_val_data">'.$v_x.'</td>';
}
echo'</tr>';

foreach($y as $k_y_t=> $v_y_t){
echo'<tr><td class="table_val_data">'.$v_y_t.'</td>';
foreach($y as $k_y=> $v_y){
    foreach($x as $k_x => $v_x){
    $i=1;
    if($k_y_t==$k_y){
        echo'<td class="table_val_data">';
    //foreach($pro_code as $k_pro_code=> $v_pro_code){
    //if($k_x.'-'.$k_y==$k_pro_code){
        echo '<input type="text" name="edit_pro_code[]" value="'.$i.'"">';
    //}
    //}             
    echo'</td>';
    }    
    } 
    $i++;
}
echo'</tr>';
}
echo'</table>';

在表格代码中我们将数据分为X和y轴,X代表$ x,y代表$ y。

整个表一次循环(假设它......!),因此只有循环时出错

第一次循环数据

$ y值就像这样Array ( [0] => bule [1] => green ) 下次循环我想要相同的数据,但我有下面的o / p

Array ( [0] => bule [1] => green [2] => L [3] => XL [4] => bule [5] => green ) 

如何自动合并。,参考下面的屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:1)

我的回答是:

foreach ($category_attribute_list_value as $key => $value) {
    foreach ($value as $key2 => $value_fial) {
        if($key==0)
        {
            $x[]=$value_fial->attribute_value;
            $x_id[]=$value_fial->attribute_value_id;
        }
    }
}

foreach ($category_attribute_list_value as $key_y => $value_y) {
    foreach ($value_y as $key2_y => $value_fial_y) {
        if($key_y==1)
        {
            $y_temp[]=$value_fial_y->attribute_value;
            $y_id[]=$value_fial_y->attribute_value_id;
        }
    }
}
            $y=array_unique($y_temp);

                //$x=array('L','XL');
                //$y=array('P','dsfs'); 
                //$x_id=array(1,2);
                //$y_id=array(5,6);
                //$xv=array_combine($x_id, $x);
                //$yv=array_combine($y_id, $y);

echo'<table cellpadding="0" cellspacing="0" border="1" class="store_inven_table">
<tr>
<td class="table_val_data">Code</td>';
foreach($x as $k_x => $v_x){
echo'<td class="table_val_data">'.$v_x.'</td>';
}
echo'</tr>';

    foreach($y as $k_y_t=> $v_y_t){
    echo'<tr><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){
            echo'<td class="store_val_data_input">';
            echo '<input type="text" name="store_pro_inv_qty[]" value="">
            <input type="text" value="'.$value_final_store->store_id.'" name="store_inventory_ids[]">';
        echo'</td>';
        }    
        } 
    }
    echo'</tr>';
    }

echo'</table>';
$x=array();$x_id=array();$y=array();$y_id=array();$value=array();

我的o / p如下: enter image description here