0

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.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55

2 Answers2

1

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 />
Jim Wright
  • 5,905
  • 1
  • 15
  • 34
  • Have you tried running it without the quotes (as in the original code)? – Nigel Ren Apr 03 '18 at 14:48
  • @NigelRen I've just updated my answer to address this. – Jim Wright Apr 03 '18 at 14:50
  • Thanks to all for their input. I have fixed the quote issue, no change. I have copied the code Jim has setup above and I can get that to work no problem. When I go back to my live code, the values will not appear. I can see the values using the print_r command, and I did get a nested loop working that looped through the array and output all of the values. But when I try and specify a set value, I get nothing. The array is being created in a Joomla page and I am passing the Post data to some code to process the Post output and then add it to an already existing array. – Glenn Tooth Apr 03 '18 at 15:27
  • I am running php 7.0.28 – Glenn Tooth Apr 03 '18 at 15:45
0

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