Create a new array based on another

I have

Array
(
    [0] => Array
        (
            [slot] => 5
            [poles] => 2
            [operational_status] => 1
            [x_coord] => 0
            [y_coord] => 186
        )

)

and am trying to create another array from it

[slots] => Array([5][7])

5 and 7 mean that those slots are covered, the slot key is the starting slot, and the poles key is the size of the breaker(its value can be 1, 2, or 3) (as seen by)


Is this a good start?

$slots  = array();

foreach($circuit_breakerArray as $slot) {
	switch($slot['poles'])
         case 1:
            array_push($slots,$slot['slot']);
         break;
         case 2:
            array_push($slots,$slot['slot'],$slot['slot'] + 2);
         break;	
         case 3:
            array_push($slots,$slot['slot'],$slot['slot'] + 2,$slot['slot'] + 4);
         break;

Well if the net result is that the $slots array ends up being an array of numbers sure. Lets assume you have two slots in $circuit_breakerArray. One with 2 poles and one with 3 poles. Your code would end up having $slots containing values like… [5, 7, 6, 8, 10]

Which is for slot 5 (with 2 poles) and slot 6 (with 3 poles). Something tells me that the ending list is not exactly what you had in mind. But if you read your code and you see the answer, you can see why this would be happening. Maybe you want to create an array of arrays? like [5 => [7], 6 => [8, 10]]?

What’s the usage for your end result? Just to know which slots have something in them?

$slots = [];
foreach($circuit_breakerArray as $slot) {
  $slots = array_merge($slots,range($slot['slot'],$slot['slot']+$slot['poles']));
}
$slots = array_unique($slots);

(If you’re doing collision detection, there are a few ways to do it, one of which being to check the size of the array before and after unique’ing)

I have a select box, which contain all the slots available.in that panel

<select class="form-control" id="Slot" name="Slot">
 <?php
if(isset($row['slot'])) { echo '<option value="'.$row['slot'].'">'.$row['slot'].'</option>'; }
for ($x = 1; $x <= $row['slots']; $x++) {
	if($x%2 == 0) {
	  echo '<option value="'.$x.'">'.$x.'</option>';
	} else {
	  echo '<option value="'.$x.'">'.$x.'</option>';		
	}
}
?>	
</select>					

But id lik to remove and slots used by the circuit breakers in the panel.
something like use a foreach() to eliminate used slots

<select class="form-control" id="Slot" name="Slot">
 <?php
if(isset($row['slot'])) { echo '<option value="'.$row['slot'].'">'.$row['slot'].'</option>'; 
$all_slots = range(1,$row['slots'];
$used_slots  = array();

 foreach($circuit_breakerArray as $slot) {
 
	switch($slot['poles']) {
         case 1:
            array_push($slots,$slot['slot']);
         break;
         case 2:
            array_push($slots,$slot['slot'],$slot['slot'] + 2);
         break;	
         case 3:
            array_push($slots,$slot['slot'],$slot['slot'] + 2,$slot['slot'] + 4);
         break;
         }  
  }
//remove any duplicates once both arrays have been merged
$available_slots = array_unique(array_merge(($all_slots,$used_slots));
 foreach($available_slots as $slot) {
 
echo '<option value="'.$slot.'">'.$slot.'</option>';
  }
?>	
</select>					

Close. You want the difference of the arrays, not the merging of them.

$used_slots = [];
foreach($circuit_breakerArray as $slot) {
  $used_slots = array_merge($used_slots,range($slot['slot'],$slot['slot']+$slot['poles']));
}
$available_slots = array_diff(range(1,$row['slots']),array_unique($used_slots));

(EDIT: If you dont want $used_slots after this, you can change it around to do the diff inside the loop instead)

1 Like

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