duanjue7508 2019-01-24 08:55
浏览 25

php echo爆炸数组

Sorry but I dont know how to explain my problem but rather I will demonstrate my problem here.

This is what I get when I used print_r()

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 )

And this is my code

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>
        }
    }
}

This is the result of the above code

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

The Expected output should be something like this

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

This is my data in db and the result of my query.

Name    |   unit_price  |   total_amount
j1      |   2           |   5000
j1      |   200         |   1000
j2      |   3           |   7500
j2      |   100         |   500
j3      |   4           |   10000
j3      |   300         |   1500

Please see the image Click me supp_dtl_1 print_r() Click me

Expected Output Click Me Database Click Me

  • 写回答

2条回答 默认 最新

  • donglijuan8227 2019-01-24 09:12
    关注

    I don't really know what exactly you want, but I hope the following code will help. You don't need to nest your second array with another for loop, instead, you can iterate it under the same loop as the first array with $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>
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助