3

I want to create a new .csv file (without opening the raw file first via fopen). So far I have tried this:

$list[] = array
(
    "Name" => "John",
    "Gender" => "M",
    "Age" => "21"
);
$timestamp0 = date("Y-m-d H:i:sa", time());
$datetime = new DateTime($timestamp0);
$datetime->setTimezone(new DateTimeZone('Asia/Jakarta'));
$timestamp = $datetime->format("Y-m-d_H-i");

$filename = __DIR__ . "/file/" . $timestamp . ".csv";

$header = array("name", "gender", "age");
file_put_contents($filename, implode("\n", $list)); // error here bcs array given :')

My questions are:

  1. How can I change array 2d to csv?

Very need your help :( Thank you so much :")

2 Answers 2

8

Using fopen with w will create the file if does not exist:

$list = [
    ["Name" => "John", "Gender" => "M"],
    ["Name" => "Doe", "Gender" => "M"],
    ["Name" => "Sara", "Gender" => "F"]
];

$fp = fopen($filename, 'w');
//Write the header
fputcsv($fp, array_keys($list[0]));
//Write fields
foreach ($list as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);

If you don't like fputcsv and fopen you can use this alternative:

$list = [
    ["Name" => "John", "Gender" => "M"],
    ["Name" => "Doe", "Gender" => "M"],
    ["Name" => "Sara", "Gender" => "F"]
];

$csvArray = ["header" => implode (",", array_keys($list[0]))] + array_map(function($item) {
    return implode (",", $item);
}, $list);

file_put_contents($filename, implode ("\n", $csvArray));

I hope this will help you.

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

5 Comments

Im sorry forget to change, Im using array multidimensional, what should I do? :')
Just update the question with your array and I will assist you in a better alternative ;)
@ismail RBOUH, -1because it not a perfect way to create CSV file. php.net/manual/en/function.fputcsv.php
@HareshVidja I'm still waiting for you to remove the downvote ! Because it was the OP request to not use open and fputcsv. However, I've updated the answer for future users ;) Thank you ;)
You have not given perfect solution first time so I have given down vote... You have to think first for give perfect solution... thanks :)
0

You can use below code

$list[]=array ("name","gender","age"); // push header here
$list[] = array("John","M","21"); // push record here

$timestamp0     = date("Y-m-d H:i:sa",time());
$datetime       = new DateTime($timestamp0);
$datetime->setTimezone(new DateTimeZone('Asia/Jakarta'));
$timestamp      = $datetime->format("Y-m-d_H-i");

$filename = __DIR__ . "/file/" . $timestamp . ".csv";

$fp = fopen($filename, 'w');
foreach ($list as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);

For more detail read php manual about CSV file. http://php.net/manual/en/function.fputcsv.php

Comments

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.