0

I have a list of <input> elements and I am trying to simplify it so the code is tidier and cleaner. Here is what I have at the moment:

if($row['replies'] == '1') {
   echo'<input type="submit" value="Locked">';
}else{
   echo'<input type="submit" value="Lock">';
}
if($row['flagged'] == '1') {
   echo'<input type="submit" value="Ignore">';
}
if($row['deleted'] == '1') {
   echo'<input type="submit" value="Deleted">';
}else{
   echo'<input type="submit" value="Delete">';
}
<input type="submit" value="Hello">
<input type="submit" value="Goodbye">

I am trying to simplify this using a multidimensional array, but I can't quite get my head around it. This is what I've tried so far:

$options = array("Lock"=>array("replies","Locked"),"Ignore"=>"flagged","Delete"=>array("deleted","Deleted"),"Hello","Goodbye");
foreach($options as $option) {
   if(in_array($option[0], array('Lock','Deleted'), true) && $row[$option[1] == '1') {
      echo'<input type="submit" value="'.$option[2].'">';
   }else{
      echo'<input type="submit" value="'.$option[0].'">';
   }
}
2
  • try foreach($array as $key=>$value) That will allow you to use the #key (which can be flagged, deleted etc.) Commented Dec 22, 2017 at 23:17
  • unless I'm going crazy you've got a typo here if(in_array($option[0], array('Lock','Deleted'), true) && $row[$option[1] == '1'), you're missing a closing ] on the last check, I think it should be if(in_array($option[0], array('Lock','Deleted'), true) && $row[$option[1]] == '1') Commented Dec 22, 2017 at 23:23

2 Answers 2

1
$submit_values =
[
    $row['replies'] == '1' ? 'locked' : 'lock',
    $row['flagged'] == '1' ? 'ignore' : '',
    $row['deleted'] == '1' ? 'deleted' : 'delete',
    'hello',
    'goodbye'
];
$submit_values = array_filter($submit_values); // Remove any empty values.
foreach($submit_values as $value)
    printf ('<input type="submit" value="%s">', $value);
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks this works and I really appreciate it! Just a few questions though... I was wondering why you didn't use $submit_values = array(...), or are they both the same thing?
Yes they are the same thing. Short array syntax has been available since version 5.4, (which has now been end of lifed php.net/eol.php). All supported versions will work with the cleaner syntax.
0
$opts = array(
    "replies" => array("Lock", "Locked"),
    "flagged" => array(null, "Ignore"),
    "deleted" => array("Delete", "Deleted")
);

foreach ($opts as $key => $values) {
    if ($row[$key] == 1) {
        echo "<input type=\"submit\" value=\"{$values[1]}\">";
    } else {
        if ($values[0]) { 
            echo "<input type=\"submit\" value=\"{values[0]}\">";
        }
    }
}

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.