PHP的回声爆炸数组

时间:2019-01-24 08:55:35

标签: php html

抱歉,但是我不知道如何解释我的问题,而是在这里演示我的问题。

这是我使用print_r()时得到的

value4

这是我的代码

Array ( [0] => 2     [1] => 200 ) 
Array ( [0] => 5000  [1] => 1000 )  
Array ( [0] => 2     [1] => 200 ) 
Array ( [0] => 5000  [1] => 1000 ) 
Array ( [0] => 2     [1] => 200 ) 
Array ( [0] => 5000  [1] => 1000 )  
Array ( [0] => 2     [1] => 200 ) 
Array ( [0] => 5000  [1] => 1000 )  
Array ( [0] => 4     [1] => 300 ) 
Array ( [0] => 10000 [1] => 1500 )  
Array ( [0] => 4     [1] => 300 ) 
Array ( [0] => 10000 [1] => 1500 )  
Array ( [0] => 4     [1] => 300 ) 
Array ( [0] => 10000 [1] => 1500 )  
Array ( [0] => 4     [1] => 300 ) 
Array ( [0] => 10000 [1] => 1500 ) 
Array ( [0] => 3     [1] => 100 ) 
Array ( [0] => 7500  [1] => 500 ) 
Array ( [0] => 3     [1] => 100 ) 
Array ( [0] => 7500  [1] => 500 ) 
Array ( [0] => 3     [1] => 100 ) 
Array ( [0] => 7500  [1] => 500 )  
Array ( [0] => 3     [1] => 100 ) 
Array ( [0] => 7500  [1] => 500 )

这是上面代码的结果

foreach ($supp_dtl_1 as $key => $value) {
    $arr = explode(',',$value->unit_price);
    $arr1 = explode(',',$value->total_amount);
    foreach($arr as $cell){
        foreach($arr1 as $cell1){
            //echo print_r($arr);
            //echo print_r($arr1);
            <td><input type="text" value="<?php echo $cell; ?>"></td>
            <td><input type="text" value="<?php echo $cell1; ?>"></td>
        }
    }
}

预期输出应该是这样

2 | 5000 | 2 | 1000 | 200 | 5000 | 200 | 1000 | 4 | 10000 | 4 | 1500 | 300 | 10000 | 300 | 3 | 7500 | 3 | 500 | 100 | 7500 | 100 | 500
2 | 5000 | 2 | 1000 | 200 | 5000 | 200 | 1000 | 4 | 10000 | 4 | 1500 | 300 | 10000 | 300 | 3 | 7500 | 3 | 500 | 100 | 7500 | 100 | 500

这是我在db中的数据以及查询的结果。

2   | 5000 |  3    | 7500   | 4   | 10000
200 | 1000 |  100  | 500    | 300 | 1500  

请查看图片Click me supp_dtl_1 print_r()Click me

预期输出Click Me 数据库Click Me

2 个答案:

答案 0 :(得分:1)

我不太清楚您到底想要什么,但是我希望下面的代码会有所帮助。 您不需要将第二个数组与另一个for循环嵌套,而是可以使用$ key在与第一个数组相同的循环下对其进行迭代。

foreach($arr as $key => $cell){
        //echo print_r($arr);
        //echo print_r($arr1[$key]);
        <td><input type="text" value="<?php echo $cell; ?>"></td>
        <td><input type="text" value="<?php echo $arr1[$key]; ?>"></td>
 }

答案 1 :(得分:0)

尝试一下

<?php
foreach ($supp_dtl_1 as $key => $value) {
    $arr = explode(',',$value->unit_price);
    $arr1 = explode(',',$value->total_amount);
    foreach($arr as $key2 => $cell){
        echo '<td><input type="text" value="'.$cell.'"></td>';
        echo '<td><input type="text" value="'.$arr1[$key2].'"></td>';
    }
}