CSV overwrite line if same date else append to new line - php

I want to write $totalToday data from API to csv file. If current date not existed, append new record for current date. I've came with following solution.
$search = date("d/m/Y");
$lines = file('data.csv');
$line_number = false;
foreach($lines as $key => $line) {
$line_number = (strpos($line, $search) !== FALSE);
}
if(!$line_number){
$entry = array(date("d/m/Y"), $totalToday);
$fp = fopen('data.csv', 'a');
fputcsv($fp, $entry);
fclose($fp);
}
My problem is $totalToday from API get updated time to time. I want to record the latest update. so I replaced $search = date("d/m/Y"); with $search = date("d/m/Y"), $totalToday now I have multiple record for same date in my data.csv. I want to overwrite the current date record with very latest data without append to new line. How to accomplish my requirement
Example data: (first rows)
date,newCases,totalToday
13/04/2020,21,110
14/04/2020,26,125
14/04/2020,30,130
I want to replace 14/04/2020,26,125 with 14/04/2020,30,130

One approach could be this:
<?php
$search = '14/04/2020';
$other_data_from_api = array(188,102);
$lines = file('data.csv');
//Create a new array and set all dates as keys
//The latest set key would be the current
$new_arr = array();
foreach($lines as $line) {
$exp = explode(',', $line);
$new_arr[$exp[0]] = array($exp[1], $exp[2]);
}
/*
So in your example:
13/04/2020,21,110
14/04/2020,26,125
14/04/2020,30,130
the array $new_arr would contain:
[13/04/2020] => Array
(
[0] => 21
[1] => 110
)
[14/04/2020] => Array
(
[0] => 30
[1] => 130
)
*/
//Rewrite the whole file with values from this new array
$fp = fopen('data.csv', 'w');
foreach($new_arr as $key=>$line) {
$entry = $key . ',' . implode(',', $line);
fputs($fp, $entry);
}
fclose($fp);
You could also:
//Rewrite the whole file with values from this new array
//And include the actual data from the API
//(Then 188,102 would be included with the data of the $search variable)
$fp = fopen('data.csv', 'w');
foreach($new_arr as $key=>$line) {
if ($search == $key) {
$entry = $search . ',' . implode(',', $other_data_from_api);
}
else {
$entry = $key . ',' . implode(',', $line);
}
fputs($fp, $entry);
}
fclose($fp);

Related

PHP Read csv data without first line

I want to store csv columns data into array
I have did this so far.
$csvfile = file('testfile.csv');
$csvData = [];
foreach ($csvfile as $key => $line) {
$csvData[] = str_getcsv($line);
}
This works fine but the first line also include in array
I want to skip first line
Remove the first element of the array with array_shift().
$csvfile = file('testfile.csv');
array_shift($csvfile);
$csvData = array_map('str_getcsv', $csvfile);
or you can do like this
$csvfile = file('testfile.csv');
unset($csvfile[0]);
$csvData = [];
foreach ($csvfile as $key => $line) {
$csvData[] = str_getcsv($line);
}

Naming each line in array in for each loop

I have multiple arrays, where I want to name each array at the end of the for each loop.
The code php code:
<?php
$csv = array_map("str_getcsv", file("translations/dk.csv"));
foreach ($csv as $line){
if ($line[1] != NULL){
$line[0] = $line[1];
}
print_r($line[0]);
print_r("<br />");
}
fclose($csv);
?>
Example of the arrays.
Array ( [0] => Search and Save [1] => Søg og Spar på Hoteller )
Array ( [0] => Where are you going? [1] => Hvor skal du hen? )
Now the output of line[0] is each time the foreach loop runs naturally with a different value. But I need to name each $line[0] on every loop so I can access them afterwards. How do I do this ?
First of all, you shouldn't use file() to split up the lines; CSV records may span multiple lines.
$f = fopen("translations/dk.csv", 'rt');
$csv = array();
while (($data = fgetcsv($f)) !== false) {
$csv[] = $data;
}
fclose($f);
Second, to select the first column from the array you can use array_column():
$results = array_column($csv, 0);
// "Search and Save", "Where are you going?"]
Maybe you're looking for something like this:
<?php
$csv = array_map("str_getcsv", file("translations/dk.csv"));
$results = array();
foreach ($csv as $k => $line){
if ($line[1] != NULL){
$line[0] = $line[1];
$results[$k] = $line[0];
}
print_r($line[0]);
print_r("<br />");
}
print_r($results);
?>

PHP: Expects parameter to be string array given

I am trying to read a csv file line by line and save its content in an array. I then parse the array using foreach to print each line.
However, when i try to send the variable(which according to me should be a string) to the deleteInstance method it prints as an array and not plain string.
I have issue sending this to Softlayer API since it throw me an error saying string expected but array given ? I am not sure what is wrong
a.csv
7381838
7381840
7381842
php
<?PHP
require_once dirname(__FILE__) . '/SoftLayer/SoapClient.class.php';
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}
// Set path to CSV file
$csvFile = 'a.csv';
$csv = readCSV($csvFile);
foreach ($csv as $value) {
var_dump($value);
print_r($value);
deleteInstance($value);
}
function deleteInstance($ccid){
$apiUsername = 'xxxxx';
$apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$cancelRightNow = true; //or false if you want to wait till the billing cycle ends
$cloudComputingInstanceId = $ccid;
print_r($cloudComputingInstanceId);
var_dump($cloudComputingInstanceId);
$client = SoftLayer_SoapClient::getClient('SoftLayer_Virtual_Guest', $cloudComputingInstanceId, $apiUsername, $apiKey);
$objectMask = new SoftLayer_ObjectMask();
$objectMask->billingItem;
$client->setObjectMask($objectMask);
$cci = $client->getObject();
$client = SoftLayer_SoapClient::getClient('SoftLayer_Billing_Item', $cci->billingItem->id, $apiUsername, $apiKey);
$billingItem = $client->getObject();
if ($billingItem != null) {
if ($cancelRightNow) {
$client->cancelService();
} else {
$client->cancelServiceOnAnniversaryDate();
}
}
}
?>
The problem is your argument to deleteInstance is an array... It looks like this:
$csv = Array(
0 => Array(0 => '7381838'),
1 => Array (0 => '7381840'),
2 => Array(0 => '7381842')
)
This is because you are parsing it as CSV which splits each line into an array based on a delimiter. This is not what you want. Instead use file to read each line into an array:
function readCSV($csvFile){
return file($csvFile);
}
$csv = readCSV('a.csv');
foreach ($csv as $value) {
// your value is now the line of the file like '7381838'
deleteInstance($value);
}
Try this and see what happens
foreach ($csv as $value) {
$svalue = $value[0];
var_dump($svalue);
print_r($svalue);
deleteInstance($svalue);
}

find and replace a specific string of a a particular line in txt file with php

I want to know that how can I replace a specific word/string of a particular line into a text file with php.
Contents of text file is as below:
1|1|1
nikki|nikki#yahoo.com|nikki
nikki|nikki#gmail.com|nikki
nikki|nikki#hotmail.com|nikki
DETAILS OF FIELDS:
COLUMN:0 = $name,
COLUMN:1 = $email,
COLUMN:2 = $nickname,
DETAILS OF REPLACEMENT:
COLUMN:0 = $newName,
COLUMN:1 = $newEmail,
COLUMN:2 = $newnickName,
From the above content you can guess that the find/search is based on the column:1. Ans if match found, than replace the column:0 OR column:2 [based on the choice].
I tried [for finding the column:1]:
$fileData = file("file.txt");
foreach($fileData as $Key => $Val) {
$Data[$Key] = explode("|", $Val);
if ( trim($Data[$Key][1]) == $email ){
unset($fileData[$Key]);
//REPLACE TAKE PLACE HERE
break;
}
}
[for replace]:
/* REPLACE NAME */
$file = "file.txt";
$oname = "|$name|";$nname = "|$newName|";
file_put_contents($file, str_replace($oname, $nname, file_get_contents($file)));
/* REPLACE NICKNAME */
$file = "file.txt";
$onickname = "|$nickname|";$nnickname = "|$newnickname|";
file_put_contents($file, str_replace($onickname, $nnickname, file_get_contents($file)));
But it was replacing all the matching "$name".
I also tried in the following way:
$fileData[$Key] = str_replace($name, $newName, $fileData[$Key]);
file_put_contents($file,$fileData);
/* $name & $newName -:> $nickname & $newnickname
But it doesn't works.
If i want to replace column:0 ["nikki"] of "nikki#gmail.com" with "nikkigmail", then the data should be:
1|1|1
nikki|nikki#yahoo.com|nikki
nikkigmail|nikki#gmail.com|nikki
nikki|nikki#hotmail.com|nikki
And, if want to replace column:2 ["nikki"] of "nikki#hotmail.com" with "hotmail", then:
1|1|1
nikki|nikki#yahoo.com|nikki
nikkigmail|nikki#gmail.com|nikki
nikki|nikki#hotmail.com|hotmail
May i get the code to be corrected ?
Here is how I would replace something like this. Instead of worrying about str_replace, why not actually modify the array returned by file?
<?php
$email = "nikki#gmail.com"; // Search email
$data = file("file.txt", FILE_IGNORE_NEW_LINES); // Read in the data
foreach($data as $key => $line) {
$bits = explode("|", $line);
if($bits[1] === $email) {
// Update this in place,
$bits[0] = "nikkigmail";
$data[$key] = implode("|", $bits);
}
}
$write = implode("\n", $data); // the data to write however you please.
Keep in mind this can also be expanded to suit your row/column needs. For example, you could use something like this.
/**
* The reason these are named differently is because they don't always
* search/replace. For example, you can find nikki#gmail.com in one row,
* but just be setting a different column in that row to a value..
*/
$match = array('col' => 1, 'str' => 'nikki#gmail.com'); // Search data at row
$update = array('col' => 0, 'str' => 'nikkigmail'); // Replace data at row
$data = file("file.txt", FILE_IGNORE_NEW_LINES); // Read in the data
foreach($data as $key => $line) {
$bits = explode("|", $line);
if($bits[$match['col']] === $match['str']) {
// Update this in place,
$bits[$update['col']] = $update['str'];
$data[$key] = implode("|", $bits);
}
}
$write = implode("\n", $data); // the data to write however you please.

creating dynamic array in php

I have following records in text file, need to extract that record form text file and treat them as seperate array variables
r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8) seperated by pipe(|)
I need to represent that as array use seperately like below
$r1= Array
(
[0] => 1
[1] => 2
[2] => 3
)
$r2=Array
(
[0] => 4
[1] => 5
[2] => 6
)
I have no idea how to do it, is it possible in php?
Just a plain regular expression to break up the string, followed by an explode on each group:
if (preg_match_all('#(\w+)=\(([\d,]*)\)#', $s, $matches)) {
foreach ($matches[2] as $i => $groups) {
$group_name = $matches[1][$i];
$$group_name = array_map('intval', explode(',', $groups));
}
}
print_r($r1);
print_r($r3);
print_r($rn);
You can use Eval
//Assuming you can pull the content from text file using fread
$temp = "r1=(1,2,3)|r2=(4,5,6)";
$temp=str_replace("=","=array",$temp);
$split=explode("|",$temp);
echo "<pre>";
foreach($split as $k=>$v){
$v="$".$v.";";
//Evaluate a string as PHP code .i.e You will get r1,r2 as a variable now which is array
eval($v);
}
print_r($r1);
print_r($r2);
$data = "r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$arr = explode("|", $data);
$finArray = array();
foreach($arr as $key=>$value)
{
$single = explode('(', $value);
$finArray[] = explode(',', str_replace(')', '', $single[1]));
}
print_r($finArray);
can be done as:
$string="r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$string=str_repla("r1=","",$string);
$yourArray=explode('|', $string);
This code will help you:--
<?php
$file = "/tmp/file1.txt"; // this is your file path
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
print $line;
$a=explode('|',$line);
print_r($a); // I have explode based on | for you...
foreach($a as $key=>$value)
{
print_r($value);
}
fclose($file);
}
?>
""or""
$a="r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$a=explode('|',$a);
print_r($a);
<?php
$file = "file.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
$str = $line;
}
$str1 = explode("|",$str);
foreach($str1 as $temp) {
$str2 = explode("=",$temp);
$data[$str2[0]] = explode(",",trim($str2[1],"()"));
}
echo '<pre>';
print_r($data);
echo '</pre>';
?>
This will do your job.

Categories