如何将else语句传递给foreach循环内的子数组

如何将else语句传递给foreach循环内的子数组

问题描述:

I wanna pass if,else condition with html data. The purpose is to check if session equals to "master" then "allow access" else "access blocked".

For this purpose i use subarray inside foreach.

Code are Below.

foreach ($list as $po_invoice) {
      $no++;
      $row = array();
      $row[] = $no;
      $row[] = $po_invoice->po_code;
      $row[] = $po_invoice->invoice_code;

      $row[]='if else here';

      $row[]='<ul class="dropdown-menu">
               <li><input type="button" class="w3-button w3-block w3-teal edit" value="Edit" id="'.$po_invoice->id.'"></li>
               <li><input type="button" class="w3-button w3-block w3-red  delete" value="Delete" id="'.$po_invoice->id.'"></li>
             </ul>



      $data[] = $row;
    }

i wanna use if-else Here

$row[]= 'if ($id->type=='super_user' OR $id->type=='user') 
    {
    echo "<span class='w3-text-red fa fa-warning '>  Access Forbidden</span>";
    } else {
    '*Some Html Here***'
    };

i used, but not working in this scenario. So please tell me how it's work in this scenario. Advance thanks who will solve my problem.

Make it simple

<?php 
$row = array();
foreach ($list as $po_invoice) {
      $no++;

      $row[] = $no;
      $row[] = $po_invoice->po_code;
      $row[] = $po_invoice->invoice_code;

     if ($id->type=='super_user' OR $id->type=='user') 
     {
        $row[] = "<span class='w3-text-red fa fa-warning '>  Access Forbidden</span>";
     }else{

        $row[]='<ul class="dropdown-menu">
               <li><input type="button" class="w3-button w3-block w3-teal edit" value="Edit" id="'.$po_invoice->id.'"></li>
               <li><input type="button" class="w3-button w3-block w3-red  delete" value="Delete" id="'.$po_invoice->id.'"></li>
             </ul>';
     }

      $data[] = $row;
}

And $row = array(); variable is written out of the scope so whenever loop run it grabs the old values

You could use a function callback instead of:

  $row[]='if else here';

  $row[]='<ul class="dropdown-menu">
           <li><input type="button" class="w3-button w3-block w3-teal edit" value="Edit" id="'.$po_invoice->id.'"></li>
           <li><input type="button" class="w3-button w3-block w3-red  delete" value="Delete" id="'.$po_invoice->id.'"></li>
         </ul>';

You could do something like this:

  $row[]=function($id) use ($po_invoice) {
    if ($id->type=='super_user' OR $id->type=='user'){ 
        echo '<ul class="dropdown-menu">
                  <li><input type="button" class="w3-button w3-block w3-teal edit" value="Edit" id="'.$po_invoice->id.'"></li>
                  <li><input type="button" class="w3-button w3-block w3-red  delete" value="Delete" id="'.$po_invoice->id.'"></li>
         </ul>';
     }else{
         echo '';
     }
 };

Then in later you just do this:

 $row($id);  //or $row[0]($id); or what have you

Sandbox

The use keyword is used to pass "extra" data into the function when it's declared, the normal arguments pass data in when it's executed.

Overall this would be a poor way to implement it, I just wanted to show it as an option. Mainly it would be poor from a maintenance and readability/standardization type of frame. What I mean by that is it would be ok if all items in the array had the same basic inputs and they where all callbacks. The issue would be matching arguments, to array items which you have no grantee of what type of item is in the array (function, html etc.) and you would have no way to know the arguments ahead of time. If you were to mix types, and arguments among various callbacks.

Hope that makes sense.

Anyway, in some situations it can be a great way to do polymorphic behaviour.

PS don't forget to put $row = array(); outside of the loop as mentioned in the other answer.

Note there is also a difference between OR and ||, in this case it doesn't apply but the word version has lower priority then the Boolean version, so you can have a situation where things run out of the order you expect if you mix the two.