0

I have some HTML formatting on my front end template that depends on the result of a foreach loop on an array, in this example in order to get the right formatting it expects the order of the panel_type_id to be 4,6,3 otherwise it builds the formatting wrong.

If the panels are added in the order of 4,6,3 then there's no problem.

Is there a way to reorder the array on load so that no matter what it follows this order?

  <div class="row middle">
  <?php foreach($panelResult as $PR): ?>
  <?php if($PR['panel_type_id'] == 4){ ?>
  <!-- Left -->
  <div class="col-lg-6"  >
    <div class="row" style="height:50%; padding-bottom: 15px;">
        <div class="col-lg-12" style="height:100%;">
            <div style="/*background-color: rgba(255, 255, 255, 0.5);*/ height: 100%; ">
        <?php echo $PR['content']?>
        </div>
        </div>
    </div>
  <?php } elseif($PR['panel_type_id'] == 6){?>
    <div class="row" style="height:50%; padding-top: 15px;">
        <div class="col-lg-12" style="height:100%;">
            <div style="/*background-color: rgba(255, 255, 255, 0.5);*/ height: 100%;">
          <?php echo $PR['content']?>
        </div>
        </div>
    </div>
  </div>
  <?php } elseif($PR['panel_type_id'] == 3){?>
  <div class="col-lg-6" >
        <div style="/*background-color: rgba(255, 255, 255, 0.5);*/ height: 100%; ">
        <?php echo $PR['content']?>
      </div>
  </div>
  <?php } ?>
  <?php endforeach; ?>
  </div><!--end row middle-->

1 Answer 1

1

You can use usort and define your own function.

Consider the following:

$panelResult = array(["panel_type_id" => 3, "content" => "CCC"], ["panel_type_id" => 4, "content" => "BBB"], ["panel_type_id" => 6, "content" => "AAA"]);

function cmp($a, $b) {
    $goodOrder = [4,6,3];
    return array_search($a["panel_type_id"], $goodOrder) - array_search($b["panel_type_id"], $goodOrder);
}

usort($panelResult, "cmp");

Now the order of $panelResult will be: 4, 6, 3 so you just keep using your original code

Sign up to request clarification or add additional context in comments.

9 Comments

So I would just change your example from id to panel_type_id and data to content?
Yes - will update my post with your var names. This is just example - notice that in your code you don't need to have Data at all...
Oh oh, I see now! So will I still have a foreach though? How exactly will I incorporate that into my loop?
This should come before the for loop - it will modify the array and then you can continue as your current code - I answered generally about re-ordering array regardless of your code
Ok, I was thinking I would have to do this within a for loop on my current array to build the key/index relationship and then use the new array in my formatting loop but maybe I'm confused
|

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.