I am using fputcsv to write to a CSV from MySQL results:
$list = array("group, variable, value \r\n");
while ($row = mysqli_fetch_assoc($result)) {
array_push($list, $row['strand_pk'] . ',' . $row['unit_name']. "\r\n");
}
$fp = fopen('../reports/data.csv', 'w');
fputcsv($fp, $list);
fclose($fp);
The array when printed on the browser page looks like:
Array
(
[0] => group, variable, value
[1] => 1,Integrated Medical Systems 1
[2] => 1,Integrated Medical Systems 2
[3] => 1,Integrated Medical Practice 1
...
)
The CSV with output looks like:
"group, variable, value
","1,Integrated Medical Systems 1
","1,Integrated Medical Systems 2
","1,Integrated Medical Practice 1
..."
What I need is the CSV to look like:
group,variable,value
1,IMP 3,40
1,IMP 2,8
1,IMP 1,54
1,IMS 2,10
What am I doing wrong here?
fputcsv expects proper, one-dimensional arrays (not comma-separated strings).
Your code should look something like this:
$list = ['group', 'variable', 'value'];
while ($row = mysqli_fetch_assoc($result)) {
$list[] = [$row['strand_pk'], $row['unit_name']]; // you're missing a value here though
}
Then you'll need to loop over the array before writing into the file:
foreach ($list as $row) {
fputcsv($fp, $row);
}
Note that you might not need to build $list at all if you only need to create the CSV with it. Use fputcsv directly within your while loop then.
Related
Originally I was taking an uploaded .csv file and iterating through the values one row at a time.. with the below while loop.
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
var_dump($data);
$importIt = $repo->impThreeCSV($which, $data);
}
I have redone the .csv upload functionality to now be a web form where I send json to the PHP file on a 'save button'. I now have a variable with multiple associative arrays in it..
$fd = json_decode($df, true);
var_dump($fd);
For instance the dump above contains multiple arrays such as below: i.e. (~300 similar arrays as below).
array (size=806)
0 =>
array (size=18)
'ATM_ID' => string 'AFF-MIL-TWR' (length=11)
'FAC_IDENT' => string 'AFF' (length=3)
'LG_NAME' => string 'BLACH BLAH ACADEMY' (length=20)
'BASIC_TP' => string 'MIL-TWR' (length=7)
.................
How can I iterate through the VALUES only in these arrays, one at a
time in the same fashion as my original while loop.. (this would be
ideal so I don't have to redo the whole back-end).
I am having issues handling the multiple arrays and getting the
values only out..
I have tried the below two attempts, I just get 'arrayarrayarray' or
similar in var_dump. Do I need to break out each array into it's
own var? What am I doing wrong here? Do I need to run a count on how
many arrays consist in my var?
$fd = json_decode($df, true);
var_dump($fd);
$data = array_values($fd);
foreach ($data as $array_key) {
echo $array_key;
}
$array_keys = array_keys($fd);
foreach ($array_keys as $array_key) {
echo $array_key;
}
P.S. No, I can't use pandas, I wish.
Assuming you want to process the data in the same way as your original piece of code, you would just use something like:
foreach ($fd as $data) {
$importIt = $repo->impThreeCSV($which, array_values($data));
}
Note I've used array_values to convert $data from an associative array to a numerically indexed one, i.e. the same format as fgetcsv returns. This may - dependent on your $repo->impThreeCSV code - not be necessary.
You need to do a foreach to loop through each array, and then do another foreach to loop through every value of the array you're looping through.
$decodedJson = [
[
'ATM_ID' => 'AFF-MIL-TWR 1',
'FAC_IDENT' => 'AFF',
'LG_NAME' => 'BLACH BLAH ACADEMY',
'BASIC_TP' => 'MIL-TWR',
],
[
'ATM_ID' => 'AFF-MIL-TWR 2',
'FAC_IDENT' => 'AFF',
'LG_NAME' => 'BLACH BLAH ACADEMY',
'BASIC_TP' => 'MIL-TWR',
],
];
foreach ($decodedJson as $array) {
foreach ($array as $item) {
print $item; # => AFF-MIL-TWR 1
}
}
I am working on a project to create 2 column CSV using PHP and i have the data stored as strings in 2 different variables . Consider below example of what i have and what i need further. Thank you in advance.
I have below 2 variables and data stored inside them. (data is separated by comma)
$title = title1,title2,title3,title4,title5..and so on
$values = value1,value2,value3,value4,value5..and so on
I need this array pattern like this
$multi_array = array
(
array(title1,value1),
array(title2,value2),
array(title3,value3),
);
I will then use the below code to generate 2 column CSV via PHP
// open the file "demosaved.csv" for writing
$file = fopen('demosaved.csv', 'w');
// save the column headers
fputcsv($file, array('Column 1', 'Column 2'));
// save each row of the data
foreach ($multi_array as $row)
{
fputcsv($file, $row);
}
// Close the file
fclose($file);
Since the values are comma separated strings, explode them into arrays:
$title = explode(",", $title);
$values = explode(",", $values);
Then you can just use the key to match up the values from each array (assuming that they are the same length):
foreach ($values as $key => $val)
{
fputcsv($file, [$title[$key], $val]);
}
Or combine them:
$multi_array = array_combine($title, $value);
foreach ($multi_array as $title => $value)
{
fputcsv($file, [$title, $value]);
}
If you're on an old version of PHP you'll need array() instead of [ ].
From the OP, since there many be duplicates in the string that would be duplicate keys: "use the below code to perform the same job and even keep the duplicate keys. PHP array_combine removes the duplicate array entries but below code keeps all entries:"
$arrKeys = array('str', 'str', 'otherStr');
$arrVals = array('1.22', '1.99', '5.17');
function foo($key, $val) {
return array($key=>$val);
}
$arrResult = array_map('foo', $arrKeys, $arrVals);
print_r($arrResult);
I'm currently having trouble with reading data from a CSV file. Now it shows the data from the CSV file like this when I print the array.
Array
(
[0] => james;large;33
)
However I want it to have it like this
Array
(
[0] => james
[1] => large
[2] => 33
)
This is the code that I'm using to read data from the CSV file
$file = fopen($file, 'r');
while ($row = fgetcsv($file) ) {
print "<pre>";
print_r($row);
print "</pre>";
}
And this is the CSV file that I'm using:
Any suggestions how I can make this work like I want to? Thanks in advance!
You have different delimiter used in CSV rather default which is ,
fgetcsv
you can define delimiter in fgetcsv function as a second parameter.
$file = fopen($file, 'r');
while ($row = fgetcsv($file, 0, ";") ) {
print "<pre>";
print_r($row);
print "</pre>";
}
In CakePHP, with the following code i am trying to Export users email in CSV.
I am getting Errors.
Code Refrence site
Error:
Notice (8): Undefined offset: 0 [APP\View\Frontusers\admin_exportemails.ctp, line 12]
Warning (2): fputcsv() expects parameter 2 to be array, null given [APP\View\Helper\CsvHelper.php, line 36]
Notice (8): Undefined offset: 1 [APP\View\Frontusers\admin_exportemails.ctp, line 12]
Warning (2): fputcsv() expects parameter 2 to be array, null given [APP\View\Helper\CsvHelper.php, line 36]
admin_exportemails.ctp
$line= $useremails[0]['Frontuser'];
$this->CSV->addRow(array_keys($line));
foreach ($useremails as $key => $useremail)
{
$line =$useremail[$key]['Frontuser'];
$this->CSV->addRow($line);
}
$filename='useremails';
echo $this->CSV->render($filename);
You're messing up your foreach. You split it up in the $key and the $useremail sub-array, which is OK. But then you iterate over it and try to access $useremail[$key]['Frontuser'] again, which is nonexistent at that point.
foreach ($useremails as $key => $useremail)
This causes the [0] and [1] in the original $useremails array to be set as $key, but you iterate over all the items over the $useremails, so you can simply:
$line = $useremail['Frontuser'];
You don't need the $key, since that's not part of the iterated item, e.g. the first time your foreach runs, it sees this:
[Frontuser] => Array
(
[name] => Rash
[email] => rash.com
)
And on the second iteration it sees this:
[Frontuser] => Array
(
[name] => John
[email] => john#gmail.com
)
So there is no [0] or [1] index anymore.
$useremail[$key]['Frontuser'];
should be
$useremail['Frontuser'];
Actually there's no need for your code to include the key in the foreach loop at all.
That's PHP 101, so for further information please refer to the manual: http://php.net/foreach
You can simply pass your data array in that function and your csv generated in webroot folder.
Note:- 1. You should put blank csv file at webroot folder.
2. you should store all information in a sub array which contain only values not Model index.
function generate_csv($data_array=array()){
// pr($data_array);die;
foreach ($data_array as $key => $value) {
$newdata[] = $key.','.$value;
}
// pr($newdata);die;
$f = fopen(APP.'webroot/csv_file.csv', 'w+');
foreach ($newdata as $line) {
fputcsv($f, array($line), ',');
}
fseek($f, 0);
fclose($f);
}
you made mistake in the for loop and iterated it in wrong way.just ommit the below line from the foreach loop.
$line =$useremail[$key]['Frontuser'];
Here have tow options
1. save csv file in folder not sent output to browser and
2. send output to browser
function generate_csv($data_array = array()) {
//$f = fopen(APP . 'webroot/files/unsaved_bars.csv', 'w+'); // for save csv file in folder not sent output to browser
$f = fopen("php://output", "a"); // for send output to browser
$headers[] = array('id','name','other_info'); // for header row
$data_array = array_merge($headers, $data_array);
foreach ($data_array as $line) {
fputcsv($f, $line, ',');
}
//fseek($f, 0);
fclose($f);
}
I am trying to copy array into another array in PHP. Then send the response as JSON output. But it copies only the last element in array multiple times. Please let me know where I am going wrong? Any help is appreciated
PHP code
stmt_bind_assoc($stmt, $resultrow);
while ($stmt->fetch()) {
$r[] = $resultrow;
print_r($resultrow);
}
echo json_encode($r);
Output from print_r($resultrow).This is correct. Values in array is different
Array( [a_id] => 1 [b_number] => 10101010 [dateandtime] => 2013-12-25 09:30:00 )
Array( [a_id] => 1 [b_number] => 20202020 [dateandtime] => 2013-12-27 11:40:00 )
Output from json_encode($r).This is incorrect. Values in array is same
[{"a_id":1,"b_number":20202020,"dateandtime":"2013-12-27 11:40:00"},
{"a_id":1,"b_number":20202020,"dateandtime":"2013-12-27 11:40:00"}]
You got the function stmt_bind_assoc from here: http://www.php.net/manual/en/mysqli-stmt.fetch.php#82742
Posted under that OP is:
"...the problem is that the $row returned is reference and not data.
So, when you write $array[] = $row, the $array will be filled up with
the last element of the dataset."
With that user's solution I came up with this to resolve your issue:
// replace your posted code with the following
$r = array();
// loop through all result rows
while ( $stmt->fetch() ) {
$resultrow = array();
foreach( $row as $key=>$value )
$resultrow[ $key ] = $value;
$r[] = $resultrow;
print_r($resultrow);
}
echo json_encode($r);
Next time you get code from a source read the comments about the source.