Avatar of Daryl Isaacs
Daryl Isaacs
 asked on

Parse array then run a if function on result

I have an array of items (cheese) in a checkbox format like so

'type' => 'checkbox',
"holder" => "div",
"class" => "",
"heading" => __("Choose your cheese topping", 'rbm_menu_item'),
'param_name' => 'cheesebox',
'value' => array( 'Cheddar'=>'Cheddar', 'Gouda'=>' Gouda', 'Bleu'=>' Bleu' ),
"description" => __("<br /><hr class='gduo'>", 'rbm_menu_item')

Open in new window

      the values of the array are displayed on a page (in a hidden div) using the heredoc declaration with an if to check if the checkbox has been used - if the return is empty the div does not show - if one of the checkboxes is "checked" then it does.

///Cheese selections
if (!empty($cheesebox)) {
          $output .= <<< OUTPUT
            <br />
            <div class="sides">Comes with: <p>{$cheesebox}</p></div>
            <br />

Open in new window

What I need to do is pull any one of the values in the array and if its in the $cheesebox the do something.

Ive tried using an ifelse like this

if ( ('$cheesebox') == "Cheddar" ){
               echo "Your topping is Cheddar";
              	elseif ( ('$cheesebox') == "Gouda" ){
               echo "Your topping is Gouda";
              	elseif ( ('$cheesebox') == "Bleu" ){
               echo "Your topping is Bleu";

Open in new window

however this does not work - I'm sure I have it wrong somewhere along the line or does the heredoc function only allow for one?

 if so is there a way to accomplish this?

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

In the second code snippet ("cheese selections") the HEREDOC tags do not match.  There may be other things in play as well, but that jumps out at me.

Here's some background information on how we handle checkboxes.

Also, it looks as if you may be using some kind of CMS or framework.  If so, please tell us what it is, thanks.
Julian Hansen

if ( ('$cheesebox') == "Cheddar" ){

Open in new window

In PHP there are three types of strings
- Single quoted - string gets dumped as is - no parsing of string - single quotes escaped with a \ double quotes allowed as is.
- Double quoted - strings are parsed and variables (optionally enclosed in {})  are replaced with their values. Double quotes escaped with a \ single quotes allowed as is
- HEREDOC - tag enclosed string - allows mixing of single and double quotes without escaping and does variable substitution.

In your example ('$cheesebox') is interpreted as a string - not its value - take the single quotes from around the variable.

You are not showing us where $cheesebox is being set - so we cannot see if your use of the if statement is correct or not.

Alternative to the if is the switch statement

switch ($cheesebox) {
    case 'Cheddar' : 
// Handle Cheddar
    case 'Gouda' :
// Handle Gouda
    case 'Bleu':
// Handle Bleu
// Handle default here (when none of the above match)

Open in new window

Ray Paseur

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Daryl Isaacs

Both of these examples work - they display the echo associated to the value name - this is based on what both Ray and Julian wrote.... I'm leaning toward the first as I can then add other divs around that particular echo.

if ($cheesebox == "Cheddar") {
echo "Your topping is Cheddar";
} else if ($cheesebox == "Gouda") {
echo "Your topping is Gouda";
}  else if ($cheesebox == "Bleu") {
echo "Your topping is Bleu";

if (in_array($cheesebox, ['Cheddar', 'Gouda', 'Bleu'])) {
echo "Your side order is {$cheesebox}";

The single and double quotes from what I understood was (in my mind) looking for a literal value in '$cheesebox' then comparing it to  whatever that was in == ""

I guess I got that round wrong

I am still on the same thing now - even though the echo works - its outside of the heredoc OUTPUT4 and throws the echo to the top of the page. Can this work inside the OUTPUT4?

I've never worked with the heredoc class before, so if this seems rudimentary, please be lenient with me...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Ray Paseur

Here are some good learning resources for those of us new to PHP.

Here is how quote marks work in PHP.

The "OUTPUT4" thing is a problem, as was noted above.  PHP Heredoc is described here.

In particular, the starting and ending tags must match for heredoc to work.

Let's try this one again: It looks as if you may be using some kind of CMS or framework.  If so, please tell us what it is, thanks.