I am trying to insert data into CSV file by storing data into array first before writing to CSV. But I am facing a problem with writing a multidimentional array to CSV file.
Here is how my input looked like :
<input type="text" name="names" placeholder="Enter Name" />
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<input type="text" name="age" placeholder="Enter Age" />
<input type="text" name="state" placeholder="Enter State" />
<label>How Did You Find Us?</label>
<input type="checkbox" name="remarks[]" value="Search Engine (eg. Google)"> Search Engine (eg. Google)
<input type="checkbox" name="remarks[]" value="Facebook"> Facebook
<input type="checkbox" name="remarks[]" value="Newspaper Ads"> Newspaper Ads
<input type="checkbox" name="remarks[]" value="Bus-Stop Ads"> Bus-Stop Ads
The following code is how I store input data and write in CSV file :
$names = filter_input(INPUT_POST, "names");
$gender = filter_input(INPUT_POST, "gender");
$age = filter_input(INPUT_POST, "age");
$state = filter_input(INPUT_POST, "state");
$remarks = $_POST['remarks'];
$inputArr[] = array(
$names, $gender, $age, $state, $remarks
);
$fp = fopen($_SERVER["DOCUMENT_ROOT"]."/student.csv", "a+");
fputcsv($fp, $inputArr);
fclose($fp);
The $inputArr
output is as follow which is a multidimentional :
Array
(
[0] => Melinda
[1] => Female
[2] => 23
[3] => united state
[4] => Array
(
[0] => Facebook
[1] => Newspaper Ads
[2] => Bus-Stop Ads
)
)
I wanted to make the array be like below array so that data can be inserted into CSV, but I don't know how to foreach the array to get the output as follow:
Array
(
[0] => Melinda
[1] => Female
[2] => 23
[3] => united state
[4] => Array
[5] => Facebook
[6] => Newspaper Ads
[7] => Bus-Stop Ads
)