dousong5492 2016-12-01 10:18 采纳率: 100%
浏览 82
已采纳

在PHP中解析多个数组

I have a CSV file that I am parsing using PHP. However, the output looks as follows:

Array(
    [0] => URL
    [1] => Value
    [2] => Author
)

Array(
    [0] => URL
    [1] => Value
    [2] => Author
)

Array(
    [0] => URL
    [1] => Value
    [2] => Author
)

And so on...

How can I parse each of these individually and/or display only one at random? I've tried using array_values, but that seems to output all arrays, not just one. Any suggestions? Feel free to let me know if there is anything else I can provide. Thanks guys.

Edit: Added some code if it helps - pretty basic.

//CSV to array and parse
function parseCSV() {
    $file = fopen('feeds/data.csv', 'r');
    while (($line = fgetcsv($file)) !== FALSE) {
        //$line is an array of the csv elements
        $arr = $line;

        $url = array_values($arr)[2];
        $author = array_values($arr)[1];

        print_r($arr);
    }

    fclose($file);      
}//end
  • 写回答

2条回答 默认 最新

  • doubi6669 2016-12-01 11:09
    关注
    <?php
    
    Class CsvRandomLine
    {
        private $line_count=0;
        private $random_element;
        private $handle;
        private $csv_arr;
    
        function __construct($file='feeds/data.csv')   
        {
            $this->handle = fopen($file, "r");
    
             $this->sort_one_element();
        }
    
    
        //CSV to array and parse
        function randomLine() 
        {
            $i = 0;
    
            // move pointer to the sorted line
            while($i < $this->random_element)
            {
              $line = fgets($this->handle);
              $i++;
            }        
    
            $line = fgetcsv($this->handle);
    
    
            //add element to $csv_arr with $url and  $author
            $this->csv_arr=array(
                                "url"    =>  $line[2],
                                "author" =>  $line[1]
                            );
    
    
        }//end
    
    
        function get($property)
        {
            return $this->csv_arr[$property];
        }
    
    
        function sort_one_element()
        {
            if($this->line_count!=0)
            {
                $max = $this->line_count;
            }
            else
            {
    
                $max = $this->countLines();
            }
    
            $max--;
    
            $n = rand( 0 , $max );    
    
            //echo $n;
    
            $this->random_element = $n;
    
            rewind($this->handle);        
    
            $this->randomLine();    
    
    
        }
    
    
        function countLines()
        {
            $linecount=0;
            while( fgets($this->handle) !== FALSE )
            {
              $linecount++;
            }
    
            $this->line_count=$linecount;
    
            return $linecount;            
        }
    
    
        function __destruct()
        {
            fclose($this->handle);
        }
    
    }    
    
    
    // How to use the class:
    
    
    $csv_r = new CsvRandomLine();
    //Construct automatic sort the first element
    
    echo $csv_r->get("author");
    echo '<br>';
    echo $csv_r->get("url");
    
    echo '<hr>';
    
    //Sorting another element
    $csv_r->sort_one_element();
    
    echo $csv_r->get("author");
    echo '<br>';
    echo $csv_r->get("url");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: