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 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题