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