Sort an array by more than one criteria - php

I have two arrays:
1- Id person (key) and qualification (value), this array have a descending order : arsort
Array
(
[61] => 02.30.00
[95] => 02.30.00
[19] => 02.01.00
[131] => 02.00.00
[58] => 01.60.00
[97] => 01.50.00
[76] => 01.40.00
[20] => 01.30.00
[112] => 01.10.00
[42] => 01.10.00
[116] => 01.04.00
}
2- ... and attempts associated to the Id person.
Array
(
[131] => 1
[58] => 1
[61] => 1
[112] => 2
[116] => 1
[42] => 1
[19] => 1
[20] => 1
[76] => 1
[97] => 1
[95] => 1
)
I need to maintain the descending order but adding ascending order by the number of the attempts. My problem is with these values:
[112] => 01.10.00 | 2
[42] => 01.10.00 | 1
How get this result?
Array
(
[61] => 02.30.00 // 1
[95] => 02.30.00 // 1
[19] => 02.01.00 // 1
[131] => 02.00.00 // 1
[58] => 01.60.00 // 1
[97] => 01.50.00 // 1
[76] => 01.40.00 // 1
[20] => 01.30.00 // 1
[42] => 01.10.00 // 1
[112] => 01.10.00 // 2
[116] => 01.04.00 // 1
)
Edit: ugly solution:
$new = array();
foreach($qualification as $k => $r)
{
$new[$k] = array(
'qualification'=> $r,
'attempt' => $attempt[$k],
'id' => $k,
);
}
foreach ($new as $key => $row)
{
$qlf[$key] = $row['qualification'];
$att[$key] = $row['attempt'];
}
array_multisort($qlf, SORT_DESC, $att, SORT_ASC, $new);
$result = array();
foreach ($new as $row)
{
$result[$row['id']] = $row['qualification'];
}
print_r($result);

uksort($qualification, function ($a, $b) use ($qualification, $attempt) {
return strcmp($qualification[$a], $qualification[$b])
?: $attempt[$a] - $attempt[$b];
});
I'm not entirely sure about which order you want to sort what in, but the above code will do; you just may have to switch $a and $b for reversing the order of one or the other.

Related

I need to create variations like woo commerce in codeigniter php

I have an array like this ( array generated dynamically from DB)
$attr = array('color'=>array('red','pink','yello','white','black','light-yellow','maroon','neal'),"brand"=>array('nike','adidas','dg','puma','neaon'),"size"=>array(8,10,11,12,13,14,15));
And I need result like
red-nike-8
red-nike-9
red-nike-10
red-nike-11
red-nike-12
red-nike-13
red-nike-14
red-nike-15
red-adidas-8
red-adidas-9
red-adidas-10
red-adidas-11
red-adidas-12
red-adidas-13
red-adidas-14
red-adidas-15
red-dg-8
red-dg-9
red-dg-10
red-dg-11
red-dg-12
red-dg-13
red-dg-14
red-dg-15
red-puma-8
red-puma-9
red-puma-10
red-puma-11
red-puma-12
red-puma-13
red-puma-14
red-puma-15
red-neaon-8
red-neaon-9
red-neaon-10
red-neaon-11
red-neaon-12
red-neaon-13
red-neaon-14
red-neaon-15
pink-nike-8
pink-nike-9
pink-nike-10
pink-nike-11
pink-nike-12
pink-nike-13
pink-nike-14
pink-nike-15
pink-adidas-8
pink-adidas-9
pink-adidas-10
pink-adidas-11
pink-adidas-12
pink-adidas-13
pink-adidas-14
pink-adidas-15........
Below is my code, This work for static but I need to developed to create dynamic structure.
$attr_color = array('red','pink','yello','white','black','light-yellow','maroon','neal');
$attr_brand = array('nike','adidas','dg','puma','neaon');
$attr_size = array(8,10,11,12,13,14,15);
$array = array();
foreach ($attr_color as $key => $value_one)
{
foreach ($attr_brand as $key => $value_two)
{
foreach ($attr_size as $key => $value_three)
{
$array[] = array($value_one,$value_two,$value_three);
}
}
}
As you have mentioned dynamic array in initial, considering that it's a array of depth two containing multiple subarrays, like below code. You can add any other subarray in this, Like I have added others array.
$attr = [
[
'color1',
'color2',
'color3',
],
[
'brand1',
'brand2',
'brand3',
],
[
'size1',
'size2',
'size3',
],
[
'other1',
'other2',
'other3',
],
];
You have to loop like this to achieve the desired result.
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return [];
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
// get combinations from subsequent arrays
$tmp = combinations($arrays, $i + 1);
$result = [];
// concat each array from tmp with each element from $arrays[$i]
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge([$v], $t) :
[$v, $t];
}
}
$finalArray = [];
foreach($result as $k => $val){
$finalArray[$k] = implode("-",$val);
}
return $finalArray;
}
$result = combinations($attr);
print_r($result);
Final result is listed below.
Array
(
[0] => color1-brand1-size1-other1
[1] => color1-brand1-size1-other2
[2] => color1-brand1-size1-other3
[3] => color1-brand1-size2-other1
[4] => color1-brand1-size2-other2
[5] => color1-brand1-size2-other3
[6] => color1-brand1-size3-other1
[7] => color1-brand1-size3-other2
[8] => color1-brand1-size3-other3
[9] => color1-brand2-size1-other1
[10] => color1-brand2-size1-other2
[11] => color1-brand2-size1-other3
[12] => color1-brand2-size2-other1
[13] => color1-brand2-size2-other2
[14] => color1-brand2-size2-other3
[15] => color1-brand2-size3-other1
[16] => color1-brand2-size3-other2
[17] => color1-brand2-size3-other3
[18] => color1-brand3-size1-other1
[19] => color1-brand3-size1-other2
[20] => color1-brand3-size1-other3
[21] => color1-brand3-size2-other1
[22] => color1-brand3-size2-other2
[23] => color1-brand3-size2-other3
[24] => color1-brand3-size3-other1
[25] => color1-brand3-size3-other2
[26] => color1-brand3-size3-other3
[27] => color2-brand1-size1-other1
[28] => color2-brand1-size1-other2
[29] => color2-brand1-size1-other3
[30] => color2-brand1-size2-other1
[31] => color2-brand1-size2-other2
[32] => color2-brand1-size2-other3
[33] => color2-brand1-size3-other1
[34] => color2-brand1-size3-other2
[35] => color2-brand1-size3-other3
[36] => color2-brand2-size1-other1
[37] => color2-brand2-size1-other2
[38] => color2-brand2-size1-other3
[39] => color2-brand2-size2-other1
[40] => color2-brand2-size2-other2
[41] => color2-brand2-size2-other3
[42] => color2-brand2-size3-other1
[43] => color2-brand2-size3-other2
[44] => color2-brand2-size3-other3
[45] => color2-brand3-size1-other1
[46] => color2-brand3-size1-other2
[47] => color2-brand3-size1-other3
[48] => color2-brand3-size2-other1
[49] => color2-brand3-size2-other2
[50] => color2-brand3-size2-other3
[51] => color2-brand3-size3-other1
[52] => color2-brand3-size3-other2
[53] => color2-brand3-size3-other3
[54] => color3-brand1-size1-other1
[55] => color3-brand1-size1-other2
[56] => color3-brand1-size1-other3
[57] => color3-brand1-size2-other1
[58] => color3-brand1-size2-other2
[59] => color3-brand1-size2-other3
[60] => color3-brand1-size3-other1
[61] => color3-brand1-size3-other2
[62] => color3-brand1-size3-other3
[63] => color3-brand2-size1-other1
[64] => color3-brand2-size1-other2
[65] => color3-brand2-size1-other3
[66] => color3-brand2-size2-other1
[67] => color3-brand2-size2-other2
[68] => color3-brand2-size2-other3
[69] => color3-brand2-size3-other1
[70] => color3-brand2-size3-other2
[71] => color3-brand2-size3-other3
[72] => color3-brand3-size1-other1
[73] => color3-brand3-size1-other2
[74] => color3-brand3-size1-other3
[75] => color3-brand3-size2-other1
[76] => color3-brand3-size2-other2
[77] => color3-brand3-size2-other3
[78] => color3-brand3-size3-other1
[79] => color3-brand3-size3-other2
[80] => color3-brand3-size3-other3
)
Thanks

PHP: HTML to PHP multidimensional associative array

Using xPath query, I am trying to extract HTML values and put them into an associative array in PHP. I am using a loop to get the rows and cells from the table. But, I can;t figure out how to get the cells in an array embedded within an array that represents the row. Basically, just transferring the table structure to an array. Ideally, it would help to assign keys for the cell data.
I tried combining $key as an array and a counter to assign key/value pair. I and xpath at different points of the structure. I can get fill up the array but I am But I just can't seem to crack it.
$cells = array();
$cell_values = array();
$key = array("MM", "DD", "TIME", "WVHT", "SwH", "SwP", "SwD", "WWH", "WWP", "WWD", "STEEPNESS", "APD");
$i = 3;
while($i <= 5){
$rows = $xpath->query('//table[#class="dataTable"][2]/tr['.$i.']');
if (!is_null($rows)){
foreach ($rows as $row) {
$cells = $row->getElementsByTagName('td');
$i++;
foreach ($cells as $cell) {
$cell_values[] = $cell->nodeValue;
$dataOut[] = array_combine($key, $cell_values);
}
}
}
}
Expected:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9]
=> [10] =>
[11] => Array ( [MM] => 02 [DD] => 17 [TIME] => 11:30 am [WVHT]
=> 3.0 [SwH] => 0.3 [SwP] => 10.5 [SwD] => SE [WWH] => 2.6 [WWP] => 8.3
[WWD] => SE [STEEPNESS] => AVERAGE [APD] => 4.4 )
//Next set of row data with $keys
[12] => Array ( [MM] => 02 [DD] => 17 [TIME] => 11:00 am [WVHT] => 3.3 [SwH]
=> 0.3 [SwP] => 10.5 [SwD] => SE [WWH] => 2.6 [WWP] => 8.3 [WWD] => SE
[STEEPNESS] => AVERAGE [APD] => 4.4 )
[13] => Array... etc.
What I Get:
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9]
=> [10] => [11] => Array ( [MM] => 02 [DD] => 17 [TIME] => 11:30 am [WVHT]
=> 3.0 [SwH] => 0.3 [SwP] => 10.5 [SwD] => SE [WWH] => 2.6 [WWP] => 8.3
[WWD] => SE [STEEPNESS] => AVERAGE [APD] => 4.4 ) [12] => [13] => [14] =>
[15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24]
=> [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] =>
[34] => [35] => )
If you define your keys as values in the $key array rather than keys, you can array_combine that with the values from the <td>s to produce the rows for your result array.
$rows = $xpath->query('//table[#class="dataTable"][2]/tr');
// define these as values so you can use them in array_combine
$key = array("WVHT", "SwH", "SwD", "WWH", "WWP", "WWD", "STEEPNESS", "AMD");
$data = array();
$cells = array();
if (!is_null($rows)) {
foreach ($rows as $row) {
$cells = $row->getElementsByTagName('td');
// get all the cell values from the row
$row_values = [];
foreach ($cells as $cell) {
$row_values[] = $cell->nodeValue;
}
// combine the keys with the values for this row and add the row to $data
$data[] = array_combine($key, $row_values);
}
}

Counting values in array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get values from this array and counting them. Let's say we have Amsterdam and I would like to count value [41, 21, 43] together and put them in a html table. The problem is that the values sometimes miss as you can see below. How can I achieve this?
Array
(
[Amsterdam] => Array
(
[41] => 2
[21] => 91
[43] => 16
[42] => 2
[20] => 30
[4] => 4
[70] => 3
[84] => 8
[46] => 4
[45] => 5
[999] => 26
[47] => 2
[3] => 8
[44] => 1
[40] => 1
[93] => 5
[56] => 3
[61] => 3
[79] => 3
[48] => 2
[50] => 5
[10] => 10
[52] => 2
[120] => 1
[95] => 1
[1] => 64
[90] => 4
[100] => 2
[101] => 1
)
[Rotterdam] => Array
(
[21] => 42
[41] => 2
[42] => 2
[46] => 1
[47] => 2
[43] => 4
[45] => 3
[4] => 1
[3] => 19
[84] => 1
[12] => 1
[20] => 14
[40] => 1
[48] => 6
[61] => 1
[52] => 1
[10] => 4
[1] => 23
[90] => 2
)
[Spaarnwoude] => Array
(
[21] => 2
)
This is what I already tried:
foreach ($headings as $h) {
echo "<th>$h</th>";
}
echo '</tr>';
foreach($cities as $cityname => $city) {
echo '<tr>';
echo "<td>$cityname</td>";
foreach (array_chunk($headings, 3) as $h) {
echo '<td>' . (isset($city[$h]) ? $city[$h] : '0') . '</td>';
}
echo '</tr>';
}
echo '</table>';
For further information you can check this link.
How to get array output in html table
You need another level of looping for each heading in the chunks.
$chunked_headings = array_chunk($headings, 3);
echo '<tr>';
foreach ($chunked_headings as $heading_group) {
echo '<th>' . implode(', ', $heading_group) . '</th>';
}
echo '</tr>';
foreach ($cities as $cityname => $city) {
echo '<tr>';
echo "<td>$cityname</td>";
foreach ($chunked_headings as $heading_group) {
$total = 0;
foreach ($heading_group as $h) {
if (isset($city[$h])) {
$total += $city[$h];
}
}
echo "<td>$total</td>";
}
}

Replace value in multi dimension array

I have array format like:
Array
(
[Australia] => Array
(
[0] => [1990,0.01],
[1] => [1991,0.02],
[2] => [1992,0.02],
[3] => [1993,0.02],
[4] => [1994,0.02],
[5] => [1995,0.02],
[6] => [1996,0.02],
[7] => [1997,0.02],
[8] => [1998,0.02],
[9] => [1999,0.02],
[10] => [2000,0.02],
[11] => [2001,0.02],
[12] => [2002,0.02],
[13] => [2003,0.02],
[14] => [2004,0.02],
[15] => [2005,0.02],
[16] => [2006,0.02],
[17] => [2007,0.02],
[18] => [2008,0.02],
[19] => [2009,empty],
[20] => [2010,empty],
[21] => [2011,empty],
[22] => [2012,empty],
[23] => [2013,empty],
[24] => [2014,empty],
[25] => [2015,empty]
)
[Pakistan] => Array
(
[0] => [1990,0.00],
[1] => [1991,0.00],
[2] => [1992,0.00],
[3] => [1993,0.00],
[4] => [1994,0.00],
[5] => [1995,0.00],
[6] => [1996,0.00],
[7] => [1997,0.00],
[8] => [1998,0.00],
[9] => [1999,0.00],
[10] => [2000,0.00],
[11] => [2001,0.00],
[12] => [2002,0.00],
[13] => [2003,0.00],
[14] => [2004,0.01],
[15] => [2005,0.01],
[16] => [2006,0.00],
[17] => [2007,0.00],
[18] => [2008,0.00],
[19] => [2009,empty],
[20] => [2010,empty],
[21] => [2011,empty],
[22] => [2012,empty],
[23] => [2013,empty],
[24] => [2014,empty],
[25] => [2015,empty]
)
)
and i want to replace 'empty' with 0 without change the array structure and elements position. I stuck how to do..
You can use array_walk_recursive function:
function replace_empty(&$item, $key) {
$item = str_replace('empty', '0', $item);
}
array_walk_recursive($your_array, 'replace_empty');
You could use the array_walk_recursive function, with a callback function that would replace empty by 0.
For example, considering your array is declared this way :
$myArray[0] = array(23, empty, 43, 12);
$myArray[1] = array(empty, empty, 53, 19);
Note : I supposed you made a typo, and your arrays are not containing only a string, but several sub-elements.
You could use this kind of code :
array_walk_recursive($myArray, 'replacer');
var_dump($myArray);
With the following callback functon :
function replacer(& $item, $key) {
if ($item === empty) {
$item = 0;
}
}
Note that :
the first parameter is passed by reference !
which means modifying it will modify the corresponding value in your array
I'm using the === operator for the comparison
And you'd get the following output :
array(
0 =>
array
0 => int 23
1 => int 0
2 => int 43
3 => int 12
1 =>
array
0 => int 0
1 => int 0
2 => int 53
3 => int 19)
I would foreach in both indices (not tested):
foreach($array as $country){
foreach($country as &$field){
if($field[1] == 'empty'){
$field[1] = 0;
}
}
}
(I assume empty is a string)
EDIT:
If this [1990,0.00] is not an array but a string, you could use str_replace instead
foreach($array as $country){
foreach($country as &$field){
$field = str_replace('empty', '0.00', $field);
}
}
}

Getting this PHP error: fputcsv() expects parameter 2 to be array

I am trying to insert an array into an array of arrays using array_splice.
array_splice($array, 0, 0, $fieldNames);
The resulting array is then converted to csv format with fputcsv. I get the following error:
fputcsv() expects parameter 2 to be array string given in...
What am I doing wrong? Must be some minor bug. Please any help. Also am I taking the right approach. My PHP knowledge of functions is not huge.
Full php code:
<?php
//MySQL login details
require('sqlAuth.php');
//return error data
function errorReport($error)
{
$data = array("error" => $error);
$json = json_encode($data);
echo $json;
die();
}
//export array to csv file
function outputCSV($data) {
$outstream = fopen("php://output", "w");
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals); // add parameters if you want
}
array_walk($data, "__outputCSV", $outstream);
fclose($outstream);
}
// open connection
$connection = mysql_connect($host, $user, $pass) or errorReport("Unable to Connect");
// select database
mysql_select_db($db, $connection) or errorReport("Unable to Select Database: " .mysql_error());
//build result set array
$array = array();
//get full questions table
$query = "SELECT q.q_id, q.trip_day, q.q0_1, q.q0_2, q.q0_3, q.q0_4, q.q0_5, q.q0_6, g.address, g.latitude, g.longitude, g.method, q.q1_1, q.q1_2, q.q1_3, q.q1_7, q.q1_9, q.q1_10, q.q1_11_1, q.q1_11_2, q.q1_11_3, q.q1_11_4, q.q2_6, q.q6_0,
q.q6_1, q.q6_1_1_1, q.q6_1_1_2, q.q6_1_1_3, q.q6_1_1_4, q.q6_1_1_5, q.q6_1_1_6, q.q6_1_1_7, q.q6_1_1_8, q.q6_1_1_9, q.q6_1_1_10, q.q6_1_1_11, q.q6_1_1_12, q.q6_1_1_13, q.q6_1_1_14,
q.q6_1_1_15, q.q6_1_1_16, q.q6_1_2_1, q.q6_1_2_2, q.q6_1_2_3, q.q6_1_2_4, q.q6_1_2_5, q.q6_1_2_6, q.q6_1_2_7, q.q6_1_2_8, q.q6_1_2_9, q.q6_1_2_10, q.q6_1_2_11, q.q6_1_2_12, q.q6_1_2_13,
q.q6_1_2_14, q.q6_1_2_15, q.q6_1_2_16, q.q6_1_3_1, q.q6_1_3_2, q.q6_2, q.q6_2_1_1, q.q6_2_1_2, q.q6_2_1_3, q.q6_2_1_4, q.q6_2_1_5, q.q6_2_1_6, q.q6_2_1_7, q.q6_2_1_8, q.q6_2_1_9,
q.q6_2_1_10, q.q6_2_1_11, q.q6_2_1_12, q.q6_2_1_13, q.q6_2_1_14, q.q6_2_1_15, q.q6_2_1_16, q.q6_2_2_1, q.q6_2_2_2, q.q6_2_2_3, q.q6_2_2_4, q.q6_2_2_5, q.q6_2_2_6, q.q6_2_2_7,
q.q6_2_2_8, q.q6_2_2_9, q.q6_2_2_10, q.q6_2_2_11, q.q6_2_2_12, q.q6_2_2_13, q.q6_2_2_14, q.q6_2_2_15, q.q6_2_2_16, q.q6_2_3_1, q.q6_2_3_2, q.q6_3, q.q6_3_1_1, q.q6_3_1_2,
q.q6_3_1_3, q.q6_3_1_4, q.q6_3_1_5, q.q6_3_1_6, q.q6_3_1_7, q.q6_3_1_8, q.q6_3_1_9, q.q6_3_1_10, q.q6_3_1_11, q.q6_3_1_12, q.q6_3_1_13, q.q6_3_1_14, q.q6_3_2_1, q.q6_3_2_2, q.q6_3_2_3,
q.q6_3_2_4, q.q6_3_2_5, q.q6_3_2_6, q.q6_3_2_7, q.q6_3_2_8, q.q6_3_2_9, q.q6_3_2_10, q.q6_3_2_11, q.q6_3_2_12, q.q6_3_2_13, q.q6_3_2_14, q.q6_3_3_1, q.q6_3_3_2
FROM questions AS q LEFT JOIN gmap_address_list AS g ON q.q0_7 = g.id";
$result_questions = mysql_query($query) or errorReport("Error in query: $query. ".mysql_error());
while ($row = mysql_fetch_array($result_questions, MYSQL_ASSOC)) {
$array[] = $row;
}
//get field names
$fieldNames = array();
$fieldNum = mysql_num_fields($result_questions);
for ($i = 0; $i < $fieldNum; $i++) {
$fieldNames[] = mysql_field_name($result_questions, $i);
}
array_splice($array, 0, 0, $fieldNames);
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=emmhts_questionnaires.csv");
header("Pragma: no-cache");
header("Expires: 0");
outputCSV($array);
// free result set memory
mysql_free_result($result_questions);
//close connection to mysql db
mysql_close($connection);
?>
Edit:
Here is a section of the array for what should be the first row of the csv file after adding print_r($array) just before outputCSV($array). It looks like the array inserted in position 0 is not closed after "[124] => q6_3_3_2", but rather there is a sub array.
Array
(
[0] => q_id
[1] => trip_day
[2] => q0_1
[3] => q0_2
[4] => q0_3
[5] => q0_4
[6] => q0_5
[7] => q0_6
[8] => address
[9] => latitude
[10] => longitude
[11] => method
[12] => q1_1
[13] => q1_2
[14] => q1_3
[15] => q1_7
[16] => q1_9
[17] => q1_10
[18] => q1_11_1
[19] => q1_11_2
[20] => q1_11_3
[21] => q1_11_4
[22] => q2_6
[23] => q6_0
[24] => q6_1
[25] => q6_1_1_1
[26] => q6_1_1_2
[27] => q6_1_1_3
[28] => q6_1_1_4
[29] => q6_1_1_5
[30] => q6_1_1_6
[31] => q6_1_1_7
[32] => q6_1_1_8
[33] => q6_1_1_9
[34] => q6_1_1_10
[35] => q6_1_1_11
[36] => q6_1_1_12
[37] => q6_1_1_13
[38] => q6_1_1_14
[39] => q6_1_1_15
[40] => q6_1_1_16
[41] => q6_1_2_1
[42] => q6_1_2_2
[43] => q6_1_2_3
[44] => q6_1_2_4
[45] => q6_1_2_5
[46] => q6_1_2_6
[47] => q6_1_2_7
[48] => q6_1_2_8
[49] => q6_1_2_9
[50] => q6_1_2_10
[51] => q6_1_2_11
[52] => q6_1_2_12
[53] => q6_1_2_13
[54] => q6_1_2_14
[55] => q6_1_2_15
[56] => q6_1_2_16
[57] => q6_1_3_1
[58] => q6_1_3_2
[59] => q6_2
[60] => q6_2_1_1
[61] => q6_2_1_2
[62] => q6_2_1_3
[63] => q6_2_1_4
[64] => q6_2_1_5
[65] => q6_2_1_6
[66] => q6_2_1_7
[67] => q6_2_1_8
[68] => q6_2_1_9
[69] => q6_2_1_10
[70] => q6_2_1_11
[71] => q6_2_1_12
[72] => q6_2_1_13
[73] => q6_2_1_14
[74] => q6_2_1_15
[75] => q6_2_1_16
[76] => q6_2_2_1
[77] => q6_2_2_2
[78] => q6_2_2_3
[79] => q6_2_2_4
[80] => q6_2_2_5
[81] => q6_2_2_6
[82] => q6_2_2_7
[83] => q6_2_2_8
[84] => q6_2_2_9
[85] => q6_2_2_10
[86] => q6_2_2_11
[87] => q6_2_2_12
[88] => q6_2_2_13
[89] => q6_2_2_14
[90] => q6_2_2_15
[91] => q6_2_2_16
[92] => q6_2_3_1
[93] => q6_2_3_2
[94] => q6_3
[95] => q6_3_1_1
[96] => q6_3_1_2
[97] => q6_3_1_3
[98] => q6_3_1_4
[99] => q6_3_1_5
[100] => q6_3_1_6
[101] => q6_3_1_7
[102] => q6_3_1_8
[103] => q6_3_1_9
[104] => q6_3_1_10
[105] => q6_3_1_11
[106] => q6_3_1_12
[107] => q6_3_1_13
[108] => q6_3_1_14
[109] => q6_3_2_1
[110] => q6_3_2_2
[111] => q6_3_2_3
[112] => q6_3_2_4
[113] => q6_3_2_5
[114] => q6_3_2_6
[115] => q6_3_2_7
[116] => q6_3_2_8
[117] => q6_3_2_9
[118] => q6_3_2_10
[119] => q6_3_2_11
[120] => q6_3_2_12
[121] => q6_3_2_13
[122] => q6_3_2_14
[123] => q6_3_3_1
[124] => q6_3_3_2
[125] => Array
(
[q_id] => 29
[trip_day] => Thursday
[q0_1] => 4
[q0_2] => 5
[q0_3] => 5
[q0_4] => 5
[q0_5] => 5
[q0_6] => 0732152589
Thanks for posting your array data. The problem is your array is multidimensional. I.e. you have a 'subarray' at 125.
Looking at the docs (http://php.net/manual/en/function.fputcsv.php) it isn't clear, but you can't use multidimensional arrays with fputcsv. If you think about it, the array you pass will be converted to one line in the csv.
You will need to think about the structure of your data, and how you expect it to be formatted in your csv, and modify your code accordingly.

Categories