Member Avatar for Ant_426

Hi. Hoping someone can explain this to me please.

$quantity = $_POST['quantity'];
          echo "<pre> -- quantity -- ";
          print_r($quantity);
          echo "</pre>";

This returns:
-- quantity --Array
(
[0] =>
[1] =>
[2] =>
[3] => 2
[4] =>
[5] =>
[6] =>
)

$selectitem = $_POST['selectitem'];
      echo "<pre> -- selectitem -- ";
      print_r($selectitem);
      echo "</pre>";

This returns:
-- selectitem --Array
(
[0] =>
[1] =>
[2] =>
[3] => ABC123 - Plain wood
[4] =>
[5] =>
[6] =>
)

$linedetail=array_combine($quantity, $selectitem);
          echo "<pre> -- linedetail -- ";
          print_r($linedetail);
          echo "</pre>";

This returns:
-- linedetail --Array
(
[] =>
[2] => ABC123 - Plain wood
)

Which then in turn causes a warning notice in the following:

foreach ($linedetail as $key => $keyvalue){
                $detail = explode(' - ',$keyvalue);
                echo "<pre> -- detail -- ";
            print_r($detail);
            echo "</pre>";
                    $code = trim($detail[0]);
                    $item = $detail[1];
            }

This returns:
-- detail --Array
(
[0] =>
)

Warning: Undefined array key 1 in ... etc.

-- detail --Array
(
[0] => ABC123
[1] => Plain wood
)

So my question is why is a blank element being created in
-- linedetail --Array
(
[] =>
[2] => ABC123 - Plain wood
)
and how can I avoid it from happening?

Recommended Answers

All 4 Replies

I can’t really provide a good answer right now because I’m typing on my iPhone from bed. However, you shouldn’t be making assumptions about the state of arrays being passed into a form (or assumptions about what any user input looks like, for that matter).

I've finally recovered enough from COVID to make it back to my computer for short periods of time.

Are you still having problems / confused with this? If so, I'll take the time to help.

commented: I'm sorry to hear you were ill. I trust you have recovered and are feeling better. Thanks for taking the time to answer. Appreciated. +0

The answer to why you have an empty array element after you execute array_combine is because your two input arrays have empty elements. As the array_combine executes it will overwrite all elements of $linedetail with empty indices and values from the two input arrays.

You might look into refactoring your form markup to deliver arrays in $_POST instead of all the backend juggling that you are presently doing, it would make your data much cleaner and your life easier.

TIP: Once you have refactored your form to deliver arrays to POST you must set it to a variable using something like the following: $some_array = filter_input(INPUT_POST, "your_post_array", FILTER_DEFAULT, FILTER_REQUIRE_ARRAY));

commented: Thanks. This is inherited code. I have come to the same conclusion. Refactoring it shall be +0

It has occurred to me that if for whatever reason, you wish to keep all of your own code with minimal refactoring you could replace:

$linedetail=array_combine($quantity, $selectitem);
with
$linedetail=array_filter(array_combine($quantity, $selectitem));

But I would recommend refactoring your form, your solution with barely coupled values appears brittle.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.