douhun8647 2014-02-25 09:19 采纳率: 0%
浏览 21
已采纳

csv php索引无法正常工作

I have one csv file "food.csv" and details are in the following order. Total rows are 10 and columns 5. And i wnat the file to be indexed by php to get the array of "Mango".

ITEM,DESC,QTY,RATE,TYPE
124,APPLE,20,300,NEW
123,MANGO,10,500,NEW
135,BANANA,30,600,OLD
148,ORANGE,12,40,NEW
111,MANGO,20,150,OLD
125,APPLE,7,100,OLD

Following is the PHP code but it is not working.

<?php
$eurl = "http://www.xyz.com/food.csv";
if (($handle = fopen ( $eurl, "r" )) !== FALSE) {
    $keys = fgetcsv ( $handle, 1000, ";" );
    while ( ($data = fgetcsv ( $handle, 1000, ";" )) !== FALSE ) {
if ($r2->id == "MANGO") {
        $found = true;
        break;
    }

        $res[] = array_combine($keys, $data);
    }
    fclose ($handle);
}
var_dump($res);
?>

I require the result in following manner. Can anybody help me in correcting the code?

When the index key is "MANGO", it should give the output as:

MANGO 10 Kgs @ 500USD (123-NEW)

MANGO 20 Kgs @ 150USD (111 -OLD)

  • 写回答

2条回答 默认 最新

  • duanpi5733 2014-02-25 09:36
    关注

    Also when you cannot visualize the array that is being produced, and it is an array and not an object add some debug code like so

    <?php
        $eurl = "http://www.xyz.com/food.csv";
        if (($handle = fopen ( $eurl, "r" )) !== FALSE) {
            $keys = fgetcsv ( $handle, 1000, "," );
    
            $output = '';
    
            while ( ($data = fgetcsv ( $handle, 1000, "," )) !== FALSE ) {
                var_dump( $data );
                if ($data[1] == "MANGO") {
                    $found = true;
    
                    $output .= sprintf( "%s %d Kgs @ %dUSD (%d-%s)
    ", 
                                       $data[1],
                                       $data[2],
                                       $data[3],
                                       $data[0],
                                       $data[4]
                                     );
                }
                // this makes no sense here, but I dont know what you were trying to do at this point
                $res[] = array_combine($keys, $data);
            }
            fclose ($handle);
        }
    
        echo $output;
    
        var_dump($res);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因