Getting this PHP error: fputcsv() expects parameter 2 to be array - php
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.
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 Array to XML
I have created a multi-dimensional array using fgetcsv from a CSV file. Using both DOMDocument and SimpleXML I am trying to create a XML file of the CSV document. The array and XML variables are being passed to a function within the same class file. The XML document is being created without any issues, but no value is passing from the array into the XML. It does work it I use a static value opposed to passing a value from the array, also if I print_r the array the structure and values are all correct. I have tried 'htmlspecialcharacters' and 'encode_UTF8' before passing the value into the XML. An example of the code is below, product is the multi-dimensional array. public function array_to_xml($product, &$xml) { foreach($product as $row) { $element = $xml->createElement("Product"); $xml->appendChild($element); $element = $xml->createElement("ID", ($row[38])); $xml->appendChild($element); } } The problem is obviously with the array but I can't find the answer. Any help would be gratefully appreciated. The output currently looks like (with not value in the ID element). Once it is working Product will have about 20 child elements. <?xml version="1.0"?> <ProductList/> <Product> <ID/> </Product> </ProductList> Example of $row when printed to screen: Array ( [0] => [1] => [2] => 6/10/2016 [3] => [4] => [5] => 7.35 [6] => N [7] => N [8] => N [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 80 [15] => 0 [16] => 80 [17] => 0 [18] => 80 [19] => N [20] => N [21] => N [22] => N [23] => 236.50 [24] => 0.00 [25] => 4.86 [26] => AFG Home Loans - Alpha [27] => 100% Offset Lo Doc Fixed [28] => 100% Offset Lo Doc 4 Year Fixed Owner Occupied [29] => 250.00 [30] => [31] => 7.35 [32] => 0.00 [33] => 4.9 [34] => N [35] => 325.00 [36] => 48 [37] => 4.52 [38] => 1-1MX78TF [39] => N [40] => [41] => [42] => N [43] => N [44] => [45] => Y [46] => 0.00 [47] => 10,000.00 [48] => 2,000,000.00 [49] => Y [50] => 30 [51] => [52] => [53] => Y [54] => 0.00 )
A couple things stand out. First, you have a syntax error on this line: $element = $xml->createElement("ID", ($row[38])); (note the errant parentheses around $row[38]. The createElement method takes a String for its second parameter. Second, you're not adding the ID to the product, but to the root XML. Fixing that, your code should look closer to this. public function array_to_xml($product, &$xml) { foreach ($product as $row) { $product= $xml->createElement("Product"); $id = $xml->createElement("ID", $row[38]); $product->appendChild($id); $xml->appendChild($product); } } If you need it as an attribute as #Barmar commented, you'd use the DOMElement->setAttribute() method, and it would look like: public function array_to_xml($product, &$xml) { foreach ($product as $row) { $product= $xml->createElement("Product"); $product->setAttribute('ID', $row[38]); $xml->appendChild($product); } }
Sort an array by more than one criteria
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.
ERROR: using '$this' when not in object context [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist Closed 9 years ago. Improve this question I have been coding and improving the code jszobody provided me that can found on this LINK which It makes me to be my refference but it results me to an error Fatal error: Using $this when not in object context PHP: <?php function getAllPossiblePermutations($mdArray, $firstTime=true, $tempArray=array()) { // initialize results array if ($firstTime) { $this->permutationsResultsArray = array(); } // find first sub array and iterate through it $thisArray = array_shift($mdArray); foreach ($thisArray as $key => $elem) { // if this number has already been used skip this possible permutation if (in_array($elem, $tempArray)) { continue; } $tempArray[] = $elem; if (count($mdArray) == 0) { $this->permutationsResultsArray[] = $tempArray; } else { $this->getAllPossiblePermutations($mdArray, false, $tempArray); } array_pop($tempArray); } } $traits = array ( array('Happy', 'Sad', 'Angry', 'Hopeful'), array('Outgoing', 'Introverted'), array('Tall', 'Short', 'Medium'), array('Handsome', 'Plain', 'Ugly') ); print_r(getAllPossiblePermutations($traits)); ?> EXPECTED OUTPUT: Array ( [0] => HappyOutgoingTallHandsome 1 => HappyOutgoingTallPlain [2] => HappyOutgoingTallUgly [3] => HappyOutgoingShortHandsome [4] => HappyOutgoingShortPlain [5] => HappyOutgoingShortUgly [6] => HappyOutgoingMediumHandsome [7] => HappyOutgoingMediumPlain [8] => HappyOutgoingMediumUgly [9] => HappyIntrovertedTallHandsome [10] => HappyIntrovertedTallPlain [11] => HappyIntrovertedTallUgly [12] => HappyIntrovertedShortHandsome [13] => HappyIntrovertedShortPlain [14] => HappyIntrovertedShortUgly [15] => HappyIntrovertedMediumHandsome [16] => HappyIntrovertedMediumPlain [17] => HappyIntrovertedMediumUgly [18] => SadOutgoingTallHandsome [19] => SadOutgoingTallPlain [20] => SadOutgoingTallUgly [21] => SadOutgoingShortHandsome [22] => SadOutgoingShortPlain [23] => SadOutgoingShortUgly [24] => SadOutgoingMediumHandsome [25] => SadOutgoingMediumPlain [26] => SadOutgoingMediumUgly [27] => SadIntrovertedTallHandsome [28] => SadIntrovertedTallPlain [29] => SadIntrovertedTallUgly [30] => SadIntrovertedShortHandsome [31] => SadIntrovertedShortPlain [32] => SadIntrovertedShortUgly [33] => SadIntrovertedMediumHandsome [34] => SadIntrovertedMediumPlain [35] => SadIntrovertedMediumUgly [36] => AngryOutgoingTallHandsome [37] => AngryOutgoingTallPlain [38] => AngryOutgoingTallUgly [39] => AngryOutgoingShortHandsome [40] => AngryOutgoingShortPlain [41] => AngryOutgoingShortUgly [42] => AngryOutgoingMediumHandsome [43] => AngryOutgoingMediumPlain [44] => AngryOutgoingMediumUgly [45] => AngryIntrovertedTallHandsome [46] => AngryIntrovertedTallPlain [47] => AngryIntrovertedTallUgly [48] => AngryIntrovertedShortHandsome [49] => AngryIntrovertedShortPlain [50] => AngryIntrovertedShortUgly [51] => AngryIntrovertedMediumHandsome [52] => AngryIntrovertedMediumPlain [53] => AngryIntrovertedMediumUgly [54] => HopefulOutgoingTallHandsome [55] => HopefulOutgoingTallPlain [56] => HopefulOutgoingTallUgly [57] => HopefulOutgoingShortHandsome [58] => HopefulOutgoingShortPlain [59] => HopefulOutgoingShortUgly [60] => HopefulOutgoingMediumHandsome [61] => HopefulOutgoingMediumPlain [62] => HopefulOutgoingMediumUgly [63] => HopefulIntrovertedTallHandsome [64] => HopefulIntrovertedTallPlain [65] => HopefulIntrovertedTallUgly [66] => HopefulIntrovertedShortHandsome [67] => HopefulIntrovertedShortPlain [68] => HopefulIntrovertedShortUgly [69] => HopefulIntrovertedMediumHandsome [70] => HopefulIntrovertedMediumPlain [71] => HopefulIntrovertedMediumUgly [72] => ) where did I go wrong?
You have a lot of referemces to class variables, like this: $this->permutationsResultsArray = array(); And PHP complains since this function is not a method in a class. It will work if you just remove this-> so you get: $permutationsResultsArray = array(); In addition when you are all done you never really return the result.. Like this: return $permutationsResultsArray; There is a problem with it though. You are recusing and you don't create that array except in the first round but your code uses it as if it was defined. BTW: Your function could be much easier with 3 foreach loops: function getCombinations($traits) { $combinations = array(''); foreach( $traits as $trait_level ) { $new_combinations = array(); foreach ( $combinations as $comb ) { foreach ( $trait_level as $trait ){ $new_combinations[] = "$comb $trait"; } } $combinations = $new_combinations; } return $combinations; }
A function doesn't have a self referential $this. Actually, removing all your $this-> references and make $tempArray a pass by reference instead of by value, your code should work... function getAllPossiblePermutations($mdArray, $firstTime=true, &$tempArray=array())
Tabular form do not show all the values inserted in textbox why? post array do not have all values why?
The problem is i am dealing with a multidimensional array. Let me explain the problem to you with simple example: Consider a table like : <form method="post" id="f"> <table class="table table-bordered" id="example"> <tbody> <?php for ($a = 0; $a <= 101; $a++) { ?> <tr> <?php for ($b = 0; $b <= 20; $b++) { ?> <td><input type="text" name="columnVal<?php echo $b; ?>[]" value="<?php echo $a . '-' . $b; ?>" /></td> <?php } ?> </tr> <?php } ?> </tbody> <input type="submit" class="btn btn-primary" value="upload" name="upload" /> </table> </form> The form above have a table which tr and td is dynamic in sense that i used php code to loop the input text and the value in it is like rownumber dash columnnumber now. I want the on submit to see the one full column textbox value. So in server script i will need, PHP CODE IS if(isset($_POST['upload'])){ print_r($_POST['columnVal0']); exit(); } so the output should be array with all first column values which is 0-0,1-0,2-0.... 101-0 right? but the problem is i get half of values. e.g 0-0,0-1,0-2 upto 45-0 only. Some How i feel its max_input_var problem but i am not sure and if you think its really that problem kindly inform me how to solve it, although i used ini_set('max_input_vars', '9999'); but its not work. And also kindly inform me if you know how to assign an array the size of ram or how to know the size of an array (in MB). I know its a bit complex problem but i hope somebody will get my question and help me solve it! Sorry for bad english!
Your code works fine, I have just improved it a little bit for the sake of readibility: HTML form: <form action="test.php" method="post" id="f"> <table class="table table-bordered" id="example"> <?php for ($a = 0; $a < 101; $a++) { echo '<tr>'; for ($b = 0; $b <= 20; $b++) { echo '<td> <input type="text" name="columnVal'.$b.'[]" value="'.$a.'-'.$b.'" /> </td>'; } echo '</tr>'; } ?> <input type="submit" class="btn btn-primary" value="upload" name="upload" /> </table> </form> Form action: <?php if(isset($_POST['upload'])){ print_r($_POST['columnVal0']); exit(); } ?> As first attempt, I got the same error message as yours, i.e. max_input_var which was a commented directive in php.ini and set at 1000. Please, uncomment it and set it to the value you need. In this way, you get the full column list you expect: Array ( [0] => 0-0 [1] => 1-0 [2] => 2-0 [3] => 3-0 [4] => 4-0 [5] => 5-0 [6] => 6-0 [7] => 7-0 [8] => 8-0 [9] => 9-0 [10] => 10-0 [11] => 11-0 [12] => 12-0 [13] => 13-0 [14] => 14-0 [15] => 15-0 [16] => 16-0 [17] => 17-0 [18] => 18-0 [19] => 19-0 [20] => 20-0 [21] => 21-0 [22] => 22-0 [23] => 23-0 [24] => 24-0 [25] => 25-0 [26] => 26-0 [27] => 27-0 [28] => 28-0 [29] => 29-0 [30] => 30-0 [31] => 31-0 [32] => 32-0 [33] => 33-0 [34] => 34-0 [35] => 35-0 [36] => 36-0 [37] => 37-0 [38] => 38-0 [39] => 39-0 [40] => 40-0 [41] => 41-0 [42] => 42-0 [43] => 43-0 [44] => 44-0 [45] => 45-0 [46] => 46-0 [47] => 47-0 [48] => 48-0 [49] => 49-0 [50] => 50-0 [51] => 51-0 [52] => 52-0 [53] => 53-0 [54] => 54-0 [55] => 55-0 [56] => 56-0 [57] => 57-0 [58] => 58-0 [59] => 59-0 [60] => 60-0 [61] => 61-0 [62] => 62-0 [63] => 63-0 [64] => 64-0 [65] => 65-0 [66] => 66-0 [67] => 67-0 [68] => 68-0 [69] => 69-0 [70] => 70-0 [71] => 71-0 [72] => 72-0 [73] => 73-0 [74] => 74-0 [75] => 75-0 [76] => 76-0 [77] => 77-0 [78] => 78-0 [79] => 79-0 [80] => 80-0 [81] => 81-0 [82] => 82-0 [83] => 83-0 [84] => 84-0 [85] => 85-0 [86] => 86-0 [87] => 87-0 [88] => 88-0 [89] => 89-0 [90] => 90-0 [91] => 91-0 [92] => 92-0 [93] => 93-0 [94] => 94-0 [95] => 95-0 [96] => 96-0 [97] => 97-0 [98] => 98-0 [99] => 99-0 [100] => 100-0 ) For array size in MB, the best answers I was able to find are reported here: php get array's data size and https://nikic.github.io/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html