【问题标题】:Parsing multiple arrays in PHP在 PHP 中解析多个数组
【发布时间】:2017-04-15 22:23:03
【问题描述】:

我有一个使用 PHP 解析的 CSV 文件。但是,输出如下所示:

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

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

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

等等……

如何单独解析每一个和/或随机只显示一个?我试过使用array_values,但这似乎输出所有数组,而不仅仅是一个。有什么建议?如果还有什么我可以提供的,请随时告诉我。谢谢大家。

编辑:如果有帮助,添加一些代码 - 非常基本。

//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

【问题讨论】:

  • array[0] ?或类似的东西?
  • 你期望的 O/P 是什么?
  • 不太清楚你应该向我们展示你的解析代码是什么问题
  • 添加了一些有用的代码。我的问题是我试图一次只显示一个数组的值 - 如果我能找出如何每次显示一个随机值就更好了。
  • 我们可以假设这些数组之一是从您的​​ csv 文件中的一行生成的吗? 问题不清楚,如果您需要任何实际帮助,更多信息/代码会很有用

标签: php arrays json


【解决方案1】:
<?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");

【讨论】:

  • 这似乎很好用,但只有当我删除它在函数内部的事实时。
  • 你说得对,我忘记调用上面代码中的函数了。现在添加:parseCSV();
  • csv(feed/data.csv) 示例:1,Alexandre,www.samurai.com 2,Captain Comando,www.capitaincomando.com 3,Puffgirl,www.powerpuffgirls.com 4,Opa江南style,www.opagangnamstyle.com
  • 更改 $this->csv_arr=array( "url" => $line[2], "author" => $line[1] );修改或添加 csv 的 cloumns。
【解决方案2】:

好吧,我会加载包含 csv 文件中所有记录的数组(第一个读取的 $line 始终是 csv 文档的标题,我将使用它来创建结果 assoc_array 键),然后如果你想要显示一个(结果数组的第一个)或随机的(只需使用array_rand())。

只是一个肮脏的例子

$result = [];
$keys = null;
while(($line = fgetcsv($f)) !== false){
    if($keys === null){
        $keys = $line;
        continue;
    }
    $row = [];
    foreach($keys as $position => $key){
        $row[$key] = $line[$position];
    }
    $result[] = $row;
}

// here you will have $result with all the records and you can get the first or do whaterver you want to show just one

如果您只需要裸数据而不需要 assoc_array,您可以这样做

$result = [];
$keys = null;
while(($line = fgetcsv($f)) !== false){
    if($keys === null){
        $keys = $line;
        continue;
    }
    $result[] = $line;
}

// 这里你将有 $result(原始数据)和 $keys(csv 的标题)

【讨论】:

  • 当然你可以在循环中执行$all_lines[] = $line; 并省去编写所有代码。
  • @RiggsFolly 的问题是你不会有一个 assoc,你将有 $all_lines[0] 作为 csv 的标题,而其他一切都不是带有数据的 assoc_array有效载荷。
  • 这给了我一个更易读的格式,但现在似乎是一个多维数组
  • @user1721449 我认为这就是你想要的,如果你只想要逐行的原始输出,你可以使用 RiggsFolly 的建议来做一些事情。我会更新答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 2020-11-23
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
相关资源
最近更新 更多