How to multiply a value like 1x4 in foreach loop

Hello,

Example resource

$panel_floors =
[
"1" => "18,13,7,3,1", // Block 1 has 5 panels
"2" => "13,7", // Block 2 has 2 panels
"3" => "18,13,7,3", // Block 3 has 4 panels
"4" => "18,13,7,3" // Block 4 has 4 panels
];

$number_of_shafts =
 [
 "1" => "1",
 "2" => "2",
 "3" => "1",
 "4" => "2"
 ];
	
$Cables_in_panel =
[
"1" => "25,30,35,32,18", // There are 5 panels in block 1 and there are 5 groups in the cable, no problem
"2" => "45,38", // There are 2 panels in block 2 and there are 2 groups in the cable, no problem
"3" => "30,28,35,38", // There are 4 panels in block 3 and there are 4 groups in the cable, no problem
"4" => "25" // Yes, here is the problem. Block 4 has 4 panels, but there is a group of cable. this means that the number of cables is equal in all panels. How to tell the foreach loop to treat it as 25,25,25,25?
];
	
	foreach ($Cables_in_panel AS $block => cables){
	
		$cable = explode(",", $cables);
		
			foreach ($cable AS $cable_subscriber){
			
			// the code inside the loop is here
			
			}
	}

Is the number of panels in the block equal to the number of cable groups in the block? First it will be checked
If there is unequal block!
Example: number of panels 4, number of cable groups 1
Example: suppose the number of wires is 25, how to tell the foreach loop to operate as 25,25,25,25?

Also, how do we tell the foreach operation by multiplying the wires of the blocks with 2 shafts by 2?

Thank you

I found something here, I guess it will, I got results in the try
I don’t know how accurate the code is

For the first problem, of filling any cables_in_panel entry, see this code -

<?php

/* if this data was originally an array or JSON encoded data that can be decoded to an array,
 you need to keep it as an array to avoid unnecessary 'data churn' since it needs to be arrays
 in order to process it
 */
// also, don't quote individual numbers, making them strings

// no quotes around individual numbers in the following -
$panel_floors =
[
1 => "18,13,7,3,1", // Block 1 has 5 panels
2 => "13,7", // Block 2 has 2 panels
3 => "18,13,7,3", // Block 3 has 4 panels
4 => "18,13,7,3" // Block 4 has 4 panels
];

$number_of_shafts =
 [
 1 => 1,
 2 => 2,
 3 => 1,
 4 => 2
 ];
	
$cables_in_panel =
[
1 => "25,30,35,32,18", // There are 5 panels in block 1 and there are 5 groups in the cable, no problem
2 => "45,38", // There are 2 panels in block 2 and there are 2 groups in the cable, no problem
3 => "30,28,35,38", // There are 4 panels in block 3 and there are 4 groups in the cable, no problem
4 => "25" // Yes, here is the problem. Block 4 has 4 panels, but there is a group of cable. this means that the number of cables is equal in all panels. How to tell the foreach loop to treat it as 25,25,25,25?
];


// function to replace a list of comma separated numbers with an array of numbers
function list_to_array($item)
{
	return array_map('intval',array_map('trim',explode(',',$item)));
}

// convert the lists to arrays
$panel_floors = array_map('list_to_array',$panel_floors);
$cables_in_panel = array_map('list_to_array',$cables_in_panel);


// for just the problem of filling an array when the number of panels is > 1 and there's a single $cables_in_panel entry -
foreach($panel_floors as $block=>$array)
{
	if(count($array) > 1 && count($cables_in_panel[$block]) == 1)
	{
		// replace the cables_in_panels single entry with a repeated entry for each panel
		$cables_in_panel[$block] = array_fill(0, count($array), $cables_in_panel[$block][0]);
	}
}

// examine the result
echo '<pre>'; print_r($cables_in_panel); echo '</pre>';

As to the second problem, post the result you expect for the example data.

The code in the link I gave above worked for me, I think it works very well.

I will share how I did it and the source code, the important thing is to use the right code.

Thank you

This code is the code that works for me and it is working
If there is something that should be more accurate, I am waiting for your suggestions

// Floors with panels according to blocks    
$panel_floors = [
            "1" => "18,13,7,3",
            "2" => "18,13,7,3",
            "3" => "18,13,7,3",
            "4" => "18,13,7,3",
            "5" => "18,13,7,3,1",
            "6" => "18,13,7,3",
            "7" => "18,13,7,3",
            "8" => "18,13,7,3,1,0",
            "9" => "18,13,7,3",
            "10" => "18,13,7,3"
        ];
    //Number of cables in the panel by blocks
    $number_cables_in_panel = [
            "1" => "25,20,15,20",
            "2" => "500,550,600,650",
            "3" => "25,20,15,20",
            "4" => "65,49,80,90",
            "5" => "100", // If the number of 5th block boards is more than one, add as many products as the number of boards.
            "6" => "25,20,15,20",
            "7" => "25,20,15,20",
            "8" => "200", // If the number of 5th block boards is more than one, add as many products as the number of boards.
            "9" => "25,20,15,20",
            "10" => "25,20,15,20"
        ];
        //How many shafts are used according to the blocks
        $number_of_shafts =
        [
            "1" => "1",
            "2" => "2", // Since the number of shafts is 2, the products will be doubled
            "3" => "1",
            "4" => "1",
            "5" => "1",
            "6" => "1",
            "7" => "1",
            "8" => "2", // Since the number of shafts is 2, the products will be doubled
            "9" => "1",
            "10" => "1"
        ];

$i = 0;
foreach($number_cables_in_panel as $Key=>$value) {
$ppppp = explode(",", $value);
$bbbbb = explode(",", $panel_floors[$Key]);
$saftsayisi = explode(",", $number_of_shafts[$Key]);
unset($sssss);
unset($sasas);
$kablo_kac_grup = count($ppppp);
$kac_pano_var = count($bbbbb);

foreach($ppppp AS $kkk=>$vvv){

$count = 0;
start:
$i++;
echo "Key=".$Key." VAL=".$vvv."<br />";

// The cable group of the block is one "35" as, and also if the shaft is two "2"
// Add as many boards to the loop iteration code below
if($saftsayisi[0] == 2 && $kablo_kac_grup == 1){
    $arti = $kac_pano_var;
}else{
    $arti = 0;
}

// If the cable group is one, rotate it as much as the number of panels. "35" as
if( $count < (($kac_pano_var-1)+$arti) && $kablo_kac_grup != $kac_pano_var ) {
  // Count
  $count++;
  // Return to start loop
  goto start;
}

// If there is a second shaft in the block, double it
// For those with more than one cable group "25,20" as
if( $count < 1 && $saftsayisi[0] == 2 ) {
  // Count
  $count++;
  // Return to start loop
  goto start;
}

}
}

I made a change
I was using wires in panel in foreach loop
However, the group of cables could be as much as the panel floor or as a group.
I was always getting reference panel floors, I used panel folds in the foreach loop
In the foreach I called the wires according to the block and index
What did this do for me?
If there is more than one panek floor but only one cable group, it turns as many loops as the panel floor and gives the products of the other floors.

foreach($number_cables_in_panel as $Key=>$value) {

This code is sufficient for the second shaft only.

// If there is a second shaft in the block, double it
// For those with more than one cable group "25,20" as
if( $count < 1 && $saftsayisi[0] == 2 ) {
  // Count
  $count++;
  // Return to start loop
  goto start;
}

Waiting for your suggestions to create the best code

Sponsor our Newsletter | Privacy Policy | Terms of Service