1

Can someone please help me I have set of data which I'm sorting out by regular expression. I have correct result, but when I'm trying to call fputcsv, script is printing only fields without header which is IP and should be printed also.

here is my code:

<?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);

this is my data result:

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)

here is the csv :

21940504    21940524
10145712    
10139358    10139458
10164438    

I need to figure that out how to put IP at first column like this :

164.38.32.100 21940504  21940524

2 Answers 2

1

I dont know what this is all about:

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
}

the value of $customer will be from the last iteration of the while loop, if I counted brackets correctly. I am pretty sure that is not what you want to be writing in the csv fputcsv($file, $customers[$customer]);. And I never seen a foreach array use an assignment like that with an array Key as $customers[$customer])

Try something like this instead:

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

This also makes no sense:

script is printing only fields without header which is IP

Headers are the first row ( typically of a CSV ) they are the names of the columns, such as IPaddress

I need to figure that out how to put IP at first column like this :

164.38.32.100,21940504,21940524

Here IP is a column, not a header. It's confusing, anyway I did my best to sort out what you want.

Or do you mean, that the other rows, somehow have a header in the array of data. I would say, the IP is the "Top Level Array Key", the other data is the values of the nested array.

Sign up to request clarification or add additional context in comments.

1 Comment

Sure, Glad I was able to sort out what you needed.
0

Thanks for looking into it.

Ip adress is showing now in csv file,

like this:

21940504    21940524    164.38.32.100
10145712    86.11.76.246    
10139358    10139458    185.31.96.130
10164438    2.126.213.238   

however it is at the last place by append and I need it to be at the first column.

1 Comment

If you are talking to me, I already updated my answer with array_unshift which will put it on the front. Although I don't see the need for you to put this "comment" as a separate answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.