Reputation: 27
I do not understand why I can't echo out specific values out of my array..
I grab the $_POST output and save it to an array, filter the array to remove empty keys, display the array, that works all OK, then I try to specify a specific value and I get nothing.
echo "Post OrderArray<pre>";
print_r($_POST[order]);
echo "</pre>";
$order_list = $_POST[order];
$order_list = array_filter(array_map('array_filter', $order_list));
echo "order_list<pre>";
print_r($order_list);
echo "</pre>";
// try to output specific values
echo $order_list[0]['code'] . " _ " . $order_list[0]['qty'] . "<br />";
Post Order Array
Array
(
[0] => Array
(
['qty'] => 3
['code'] => 29468
)
[1] => Array
(
['qty'] =>
)
[2] => Array
(
['qty'] =>
)
[3] => Array
(
['qty'] =>
)
[4] => Array
(
['qty'] =>
)
)
Filtered Order_list Array
Array
(
[0] => Array
(
['qty'] => 3
['code'] => 29468
)
)
_
I think I should be getting "29468 _ 3" but I am not getting any values out of the array.
Upvotes: 0
Views: 49
Reputation: 27
I found my error, when I was creating the array I used:
$checkboxdata = "<input type=\"checkbox\" name=\"order[$x]['code']\" value=\"$sku\" />$sku";
$qty_checkbox = "<input type=\"text\" name=\"order[$x]['qty']\" class=\"spinner-1\" value=\"\" />";
so my array looked like:
Array
(
[0] => Array
(
['qty'] => 1
['code'] => 29468
Notice the quote marks.
When I edited my code to :
$checkboxdata = "<input type=\"checkbox\" name=\"order[$x][code]\" value=\"$sku\" />$sku";
$qty_checkbox = "<input type=\"text\" name=\"order[$x][qty]\" class=\"spinner-1\" value=\"\" />";
By removing the quotes, I can now access and get my individual values.
I knew it was something silly, and I was right.
Thanks to all for their assistance.
G
Upvotes: 0
Reputation: 6058
I copied your sample and added quotes when you are accessing $_POST
and am getting the expected output.
If you do not use quotes you will get a notice similar the following depending on your PHP version:
Notice: Use of undefined constant order - assumed 'order' in /private/tmp/post.php on line 13
If you are using PHP 7.1.8 or less the script should still execute without issue. If you are using PHP 7.2 then this behaviour has been deprecated and will not run. You can find more info about this in this related answer.
Working script (in PHP 7.1.8):
// Manually set this for testing...
$_POST = [
'order' => [
['qty' => 3, 'code' => 29468],
['qty' => null],
['qty' => null],
['qty' => null],
['qty' => null],
]
];
echo "Post OrderArray<pre>";
print_r($_POST['order']); // Added quotes
echo "</pre>";
$order_list = $_POST['order']; // Added quotes
$order_list = array_filter(array_map('array_filter', $order_list));
echo "order_list<pre>";
print_r($order_list);
echo "</pre>";
// try to output specific values
echo $order_list[0]['code'] . " _ " . $order_list[0]['qty'] . "<br />";
Output:
php post.php
Post OrderArray<pre>Array
(
[0] => Array
(
[qty] => 3
[code] => 29468
)
[1] => Array
(
[qty] =>
)
[2] => Array
(
[qty] =>
)
[3] => Array
(
[qty] =>
)
[4] => Array
(
[qty] =>
)
)
</pre>order_list<pre>Array
(
[0] => Array
(
[qty] => 3
[code] => 29468
)
)
</pre>29468 _ 3<br />
Upvotes: 1