【问题标题】:php fputcsv of nested array.嵌套数组的 php fputcsv。
【发布时间】:2017-10-29 10:30:47
【问题描述】:

谁能帮我,我有一组数据,我正在通过正则表达式整理出来。我有正确的结果,但是当我尝试调用 fputcsv 时,脚本只打印没有标题的字段,即 IP 并且也应该打印。

这是我的代码:

<?php

$handle = fopen("data2.txt", "r");
if (!$handle) {
  exit;
}

$customers = array();

while (($line = fgets($handle)) !== false) {

    if(preg_match('/-([0-9]*)-pdt.html.*rd:(.*)\|X/i',$line,$output)){
      $product = $output[1];
      $customer = $output[2];   

        if(!isset($customers[$customer])){

            $customers[$customer] = array($product);

          } else {
                if (!in_array($product, $customers[$customer])){

            $customers[$customer][] = $product;

                }
            }       
        }
    }
$file = fopen('file.csv', 'w+');
foreach ($customers as $customers[$customer]) 
{   
    fputcsv($file, $customers[$customer]);
}
var_dump ($customers);
fclose($handle);

这是我的数据结果:

array (size=10)
  '164.38.32.100' => 
    array (size=2)
      0 => string '21940504' (length=8)
      1 => string '21940524' (length=8)
  '86.11.76.246' => 
    array (size=1)
      0 => string '10145712' (length=8)
  '185.31.96.130' => 
    array (size=2)
      0 => string '10139358' (length=8)
      1 => string '10139458' (length=8)
  '2.126.213.238' => 
    array (size=1)
      0 => string '10164438' (length=8)

这里是 csv:

21940504    21940524
10145712    
10139358    10139458
10164438    

我需要弄清楚如何将 IP 放在第一列,如下所示:

164.38.32.100 21940504  21940524

【问题讨论】:

    标签: php arrays csv fputcsv


    【解决方案1】:

    我不知道这是怎么回事:

    while (($line = fgets($handle)) !== false) {
      ....
         $customer = $output[2];
      ....
    } //end while loop, with $customer defined in it, 
    
    foreach ($customers as $customers[$customer]) //<-- whats this about
    {   
        fputcsv($file, $customers[$customer]);  //<-- this also makes no sense
    }
    

    如果我正确计算了括号,$customer 的值将来自 while 循环的最后一次迭代。我很确定这不是您想在 csv fputcsv($file, $customers[$customer]); 中写的内容。而且我从未见过一个 foreach 数组使用像这样的赋值与数组 Key as $customers[$customer])

    试试这样的方法:

     foreach ($customers as $ipAddress => $customer) 
     {   
         array_unshift($customer, $ipAddress); //prepend IP address to inner array
         fputcsv($file, $customer);
     }
    

    这也没有意义:

    脚本仅打印不带 IP 标头的字段

    标题是第一行(通常是 CSV),它们是列的名称,例如 IPaddress

    我需要弄清楚如何将 IP 放在第一列,如下所示:

    164.38.32.100,21940504,21940524

    这里的 IP 是一列,而不是标题。很混乱,反正我已经尽力整理出你想要的东西了。

    或者你的意思是,其他行在数据数组中有一个标题。我会说,IP是“顶级数组键”,其他数据是嵌套数组的值。

    【讨论】:

    • 当然,很高兴我能够整理出您需要的东西。
    【解决方案2】:

    感谢您的调查。

    IP 地址现在显示在 csv 文件中,

    像这样:

    21940504    21940524    164.38.32.100
    10145712    86.11.76.246    
    10139358    10139458    185.31.96.130
    10164438    2.126.213.238   
    

    但是,它通过附加位于最后一个位置,我需要它位于第一列。

    【讨论】:

    • 如果你在和我说话,我已经用array_unshift 更新了我的答案,它将把它放在前面。尽管我认为您不需要将此“评论”作为单独的答案。
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2013-08-08
    • 2017-04-03
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多