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 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来