0

I have an array of arrays that get populated when a form is submitted.

$groceries = array(
    "Veggies" => array( 
        "Cabbage, ear(s)" => $_POST['cabbageQTY'],
        "Carrots, bag(s)" => $_POST['carrotQTY'],
        "Tomatoes, vine(s)" => $_POST['tomatoeQTY']
    ),
    "Meats" => array( 
        "Ground Beef, lb(s)" => $_POST['groundBeefQTY'],
        "Steak, lb(s)" => $_POST['steakQTY'],
        "Pork, lb(s)" => $_POST['porkQTY']
    ),
    "Desserts" => array(
        "Ice Cream, gallon(s)" => $_POST['iceCreamQTY'],
        "Jello, box(es)" => $_POST['jelloQTY'],
        "Pie, box(es)" => $_POST['pieQTY']
    )
);

If all the fields come up null, is there a way to check to see if the values will be empty?

I read somewhere else you can try and do this.

!array_filter($groceries)

But I think this would work for a 1 dimentional array. Anyway to do this in a 2d array? If you guys could tell me how to do this, it would be awesome

Thank you in advance.

2
  • 1
    combine array_walk() with array_filter() Commented May 10, 2014 at 0:43
  • perhaps look into count() as well to count the content in the array. Example if( count($groceries) > 0 ) {echo "My groceries array has something!";} Commented May 10, 2014 at 2:21

2 Answers 2

1

first you can use <input type="text" name="cabbageQTY" required> . Then user cannot leave them blank. After if its not work for you you can try to lookup all posts at the same time if they are null or not.

foreach ($_POST as $key => $value)
  if  $value==(null) echo $key.' value is null';
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure the question is explained fully. If this is only being populated from a form POST, then there is no NULL, they should exist as empty strings. If you want to save an empty string text like "Empty", then maybe do something like this:

$blank = "Empty";
...
"Ground Beef, lb(s)" => $_POST['groundBeefQTY'] == "" ? $blank : $_POST['groundBeefQTY'],
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.