Quoting through an array of different lengths in PHP

I have an order form with inputs like this:

<input type="text" name="booking[Sandwich][Roastbeef][qty]"  />
<input type="text" name="booking[Sandwich][Cheese][qty]"  />
<input type="text" name="booking[Pizza][Classic][qty]"  />
<input type="text" name="booking[Pizza][Special][qty]"  />
<input type="text" name="booking[Coffee][qty]"  />

      

I am having problems handling arrays correctly.

This is the conclusion I would like to have:

<h2>Sandwich</h2>
<p><strong>Roastbeef:</strong> 10</p>
<p><strong>Cheese:</strong> 5</p>

<hr>
<h2>Coffee</h2>
<p><strong>Quantity:</strong> 15</p>

      

If the entire pizza entry is empty, the Pizza header should not be printed! It's the same with the "coffee" or "sandwich" group. If the order contains nothing in the group, the title should not be printed.

I cannot write specific tests for each input since I have 200 of them.

Here's what I tried to do:

$booking = $_POST['booking'];   

//First check if there is one or more input that is not empty
if (!empty($booking)) {

  foreach ($booking as $type => $items) {
    if (count(array_filter($items))) {
        $order .= "<hr>\n<h2>" . ucfirst($type) . ":</h2>\n";
    }
    foreach ($items as $name => $qty) {
        if ($qty > "0"){
            $order .= "<p><strong>" . ucfirst($name) . ":</strong> "  . $qty . "</p>\n";
        }
    }
  }
}

      

This code only works when the array is two keys long. I can't seem to wrap my brain around how to deal with other lengths. Any help would be great!

Edit:

With the answer from @treegarden, I have almost what I need. Now I just need to have some kind of validation if "group" is empty then <h2>

shouldn't be printed. if (count(array_filter($entry)))

works without printing anything if the group is empty, but only for those inputs that only have two keys.

if (!empty($booking)) {

    foreach($booking as $key=>$entry) {
        if (count(array_filter($entry))) {

            echo "<h2>$key</h2>"; //Should only be printed if one or more inputs in the group are not empty

            foreach($entry as $key=>$subEntry) {
                if(is_array($subEntry) && $subEntry['qty'] > 0) {
                    echo "<p><strong>$key:</strong>" . $subEntry['qty'] . "</p>";
                } elseif(!is_array($subEntry) && $subEntry > 0) {
                    echo "<p><strong>Quantity:</strong>  $subEntry</p>";
                }
            }
            echo '<hr/>';
        }
    }        


}  

      

+3


source to share


2 answers


Maybe try recursion , from the snippet example:

<?php
class RecursiveArrayOnlyIterator extends RecursiveArrayIterator {
  public function hasChildren() {
    return is_array($this->current());
  }
}
?>

      



else a simple straight forward way is to assume you have three or more nested loops, keep check - $ value in $ kv using is_array () , which is done by calling a function.

0


source


try it



$booking = $_POST['booking'];

if (!empty($booking)) {

  foreach ($booking as $type => $items) {
    if (count(array_filter($items))) {
        $order .= "<hr>\n<h2>" . ucfirst($type) . ":</h2>\n";
    }
    foreach ($items as $name => $qty) {
        if (is_array($qty)) {
           foreach ($qty as $qt) {
           if ($qty > "0"){
            $order .= "<p><strong>" . ucfirst($name) . ":</strong> "  . $qt. "</p>\n";
            }
        }
        } else {
        if ($qty > "0"){
            $order .= "<p><strong>" . ucfirst($name) . ":</strong> "  . $qty . "</p>\n";
        }
        }
    }
  }
}

      

0


source







All Articles