1

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) &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="remarks[]" value="Facebook"> Facebook &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="remarks[]" value="Newspaper Ads"> Newspaper Ads &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="remarks[]" value="Bus-Stop Ads"> Bus-Stop Ads &nbsp;&nbsp;&nbsp;

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
)
5
  • can you add a small csv sample with the header? Commented Aug 24, 2017 at 6:38
  • 1
    Remove $remark from your $inputArray and then use array_merge() to add them instead: $inputArray = array_merge($inputArrray, $remarks);. That should create a one dimension array with all the values. Commented Aug 24, 2017 at 6:39
  • Btw, your code should produce another level in your current array, since you're using $inputArr[] = ... instead of $inputArr = ... Commented Aug 24, 2017 at 6:42
  • @MagnusEriksson your solution solved my problem. Thank you! You may want to put that as a solution so that I can mark it as an answer. Commented Aug 24, 2017 at 6:45
  • 2
    You have a couple of answers below that fits the bill, you can accept either of those two. No need to add another answer, doing the same. :-) Commented Aug 24, 2017 at 6:50

2 Answers 2

1

Use array_merge, also I don't think you need [] while assigning to inputArr:

$inputArr = array_merge(array($names, $gender, $age, $state), $remarks);
$fp = fopen($_SERVER["DOCUMENT_ROOT"]."/student.csv", "a+");

fputcsv($fp, $inputArr);
fclose($fp);
Sign up to request clarification or add additional context in comments.

Comments

0

Just merge the values before pushing them:

Code: (Demo)

$names="joe";
$gender="smith";
$age="18";
$state="WA";
$remarks=array('Facebook','Newspaper Ads','Bus-Stop Ads');

$inputArr=array_merge(array($names, $gender, $age, $state),$remarks);
var_export($inputArr);

Output:

array (
  0 => 'joe',
  1 => 'smith',
  2 => '18',
  3 => 'WA',
  4 => 'Facebook',
  5 => 'Newspaper Ads',
  6 => 'Bus-Stop Ads',
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.