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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里