Arrays in a for loop?

I have

    [0] => Array
            [power_supply_id] => 1
            [power_strip_id] => 1
            [receptacle_id] => 
            [outlett_placement] => 7
            [circuit_breaker_id] => 
            [power_panel_id] => 
            [pdu_id] => 


and am using this

for ($x = 1; $x <= 2; $x++) {
  if(sizeof($power_supplies) >= 1) {
	foreach($power_supplies AS $power_supply) {

Each device can have 2 power supplies, but only 1 is present (the array)… how do I alter the foreach loop so I can target the one in the array?
I tried

foreach($power_supplies[$x - 1] AS $power_supply) 

to no avail.

The apparent answer, in the form of a question (Jeopardy theme can be heard playing), why are you using a foreach(){} loop at all? What result are you trying to produce if there’s only one PS and what result are you trying to produce if there’s two?

Edit: Let me add another question. You are looping for x = 1, to 2. Does that specifically matter? Would 0 to 1 work?

ok, heres the sitchuation
I have

You see how you can have 2 power trails (blue) but I only have 1 currently.
I want to only list the one (by having the image of a power strip) and still be able to add a power strip below
the top trail has a power strip, while the other doesnt…
yep, i have to be prepared if there are 4 power trails

//number to use for iteration

While you didn’t actually answer the question, maybe this is the solution -

// loop over the number of 'elements' to display
foreach(range(1,$row['total_plugs']) as $x)
	// test if there's data for the current $x value
		// do whatever you want with the sub-array of data in $power_supplies[$x-1]
1 Like

dang, I have to learn to think like that

The theme there is you are looping to produce some number of display elements. As you are doing that, you test if there’s data corresponding to each element.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.