# Search within the array and find its coefficients

Hello,

I’m trying to calculate Patch Panel based on optic count

I tried to achieve the result as below but it doesn’t always work correctly
I am open to your suggestions, If my codes are correct, where is wrong?

``````\$optic = 301; // Number of Optical Dots
\$patchpanelarray = array("1=>12", "2=>24", "3=>48", "4=>72", "5=>96" ); // Number of Patch Panels and Ports

// Let's find the most port patch panel
\$patchpanel = end(\$patchpanelarray); // 96
// Is the most multi-port patch panel optical port large?
if(\$optic >= \$patchpanel){
// How many 96 port panels from the sample optical point count
\$i = 0;
while(\$i < \$optic){
\$i=\$i+\$patchpanel;
if(\$i <= \$optic){
\$carpan[] = \$i;
}
}
// Calculating the array we get how many panels there are
\$numberofpanels = count(\$carpan);
// We find the number increasing from the coefficient of 96
\$remainingnumber = (\$optic-end(\$carpan)); // We find that the extra number is 13
// Quantity and id array creation for 96 port patch panel
\$patchpanel_id = array(\$numberofpanels=>array_search(\$patchpanel, \$patchpanelarray);

//Now let's find the patch panel for 13 optical points
if(\$remainingnumber >0){
// From the function, we want the patch panel to give 13 ports and more.
// Since there are no 13 ports, it has 24 and will give 24 ports.
\$secondpatchpanel = closest(\$remainingnumber, \$patchpanelarray);
// Now let's get the quantity and id of the found products into an array
\$quantity = 1; //There is always 1 available here
\$patchpanel_id = array(\$quantity=>array_search(\$secondpatchpanel, \$patchpanelarray);
}
}else{ // If the highest port is less than 96 ports, the number of optical points
\$quantity = 1; //There is always 1 available here
\$patchpanel_id = array(\$quantity=>array_search(\$optic, \$patchpanelarray);
}

function closest(\$number, \$array) {
sort(\$array);
foreach (\$array as \$a) {
if (\$a >= \$number) return \$a;
}
return end(\$array); // or return NULL;
}
``````

I did something like this, but I don’t know how accurate it is

Adem, every place you have an “array_search”, you left off the last " ) " ! The array never ends.
Should be:

But, you are locking in 301 as your test and therefore the test will always result in nothing since it is larger than the max value in the array. Hope this helps.

To clarify; are you trying to find what patch panels you’d need for a given count? So 301 would need 3 x 5 (96) and 1 x 2 (24)?

Yes

This error occurred while typing here

I found the bug, I guess I need to do some more testing
I’m creating a series by pulling the patch panels from the database, but the array was intertwined and I tried to solve the problem as follows (of course amateurish ;)))

``````while(){
\$ids[] = \$row['id'];
\$ports[] = \$row['port'];
}
\$patchpanelarray = array_combine(\$ids,\$ports);
\$patchpanelarray[0] //This is how I use
``````

at the end of the code I use continue 1; for function and other brand so I can create separate tables for brands
test without style

How would you code for it?

Glad you solved it. The code is not important to us if you solved it. If someone needs this type of code, I am sure they will post and ask for it. Glad you solved another problem !

1 Like

Thank you, I was trying to come up with a very indirect solution before
Upgrading from here to the code above is perfect for me (amateurish)

``````switch(\$toplam_optik_noktasi){
case \$toplam_optik_noktasi <= 12 : \$hedefsecenek= array(
"12"); break;
case \$toplam_optik_noktasi <= 24 : \$hedefsecenek= array(
"24"); break;
case \$toplam_optik_noktasi <= 48 : \$hedefsecenek= array(
"48", "12-12", "72"); break;
case \$toplam_optik_noktasi <= 72 : \$hedefsecenek= array(
"72", "96", "48-24"); break;
case \$toplam_optik_noktasi <= 96 : \$hedefsecenek= array(
"96", "48-48", "72-24"); break;
case \$toplam_optik_noktasi <= 108 : \$hedefsecenek= array(
"96-12", "96-24", "48-48-24"); break;
case \$toplam_optik_noktasi <= 120 : \$hedefsecenek= array(
"96-24", "72-72"); break;
case \$toplam_optik_noktasi <= 144 : \$hedefsecenek= array(
"72-72", "96-48"); break;
case \$toplam_optik_noktasi <= 168 : \$hedefsecenek= array(
"96-72", "96-96"); break;
case \$toplam_optik_noktasi <= 192 : \$hedefsecenek= array(
"96-96", "72-72-72"); break;
case \$toplam_optik_noktasi <= 216 : \$hedefsecenek= array(
"96-96-24", "72-72-72"); break;
case \$toplam_optik_noktasi <= 288 : \$hedefsecenek= array(
"96-96-96"); break;
case \$toplam_optik_noktasi <= 300 : \$hedefsecenek= array(
"96-96-96-12"); break;
case \$toplam_optik_noktasi <= 312 : \$hedefsecenek= array(
"96-96-96-24"); break;
case \$toplam_optik_noktasi <= 336 : \$hedefsecenek= array(
"96-96-96-48"); break;
case \$toplam_optik_noktasi <= 360 : \$hedefsecenek= array(
"96-96-96-72"); break;
case \$toplam_optik_noktasi <= 384 : \$hedefsecenek= array(
"96-96-96-96"); break;
case \$toplam_optik_noktasi <= 396 : \$hedefsecenek= array(
"96-96-96-96-12"); break;
case \$toplam_optik_noktasi <= 408 : \$hedefsecenek= array(
"96-96-96-96-24"); break;
case \$toplam_optik_noktasi <= 432 : \$hedefsecenek= array(
"96-96-96-96-48"); break;
case \$toplam_optik_noktasi <= 456 : \$hedefsecenek= array(
"96-96-96-96-72"); break;
case \$toplam_optik_noktasi <= 480 : \$hedefsecenek= array(
"96-96-96-96-96"); break;
case \$toplam_optik_noktasi <= 492 : \$hedefsecenek= array(
"96-96-96-96-96-12"); break;
case \$toplam_optik_noktasi <= 504 : \$hedefsecenek= array(
"96-96-96-96-96-24"); break;
case \$toplam_optik_noktasi <= 528 : \$hedefsecenek= array(
"96-96-96-96-96-48"); break;
case \$toplam_optik_noktasi <= 552 : \$hedefsecenek= array(
"96-96-96-96-96-72"); break;
case \$toplam_optik_noktasi <= 576 : \$hedefsecenek= array(
"96-96-96-96-96-96"); break;
case \$toplam_optik_noktasi <= 588 : \$hedefsecenek= array(
"96-96-96-96-96-96-12"); break;
case \$toplam_optik_noktasi <= 600 : \$hedefsecenek= array(
"96-96-96-96-96-96-24"); break;
case \$toplam_optik_noktasi <= 624 : \$hedefsecenek= array(
"96-96-96-96-96-96-48"); break;
case \$toplam_optik_noktasi <= 648 : \$hedefsecenek= array(
"96-96-96-96-96-96-72"); break;
case \$toplam_optik_noktasi <= 672 : \$hedefsecenek= array(
"96-96-96-96-96-96-96"); break;
case \$toplam_optik_noktasi <= 684 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-12"); break;
case \$toplam_optik_noktasi <= 696 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-24"); break;
case \$toplam_optik_noktasi <= 720 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-48"); break;
case \$toplam_optik_noktasi <= 744 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-72"); break;
case \$toplam_optik_noktasi <= 768 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-96"); break;
case \$toplam_optik_noktasi <= 780 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-96-12"); break;
case \$toplam_optik_noktasi <= 792 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-96-24"); break;
case \$toplam_optik_noktasi <= 816 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-96-48"); break;
case \$toplam_optik_noktasi <= 840 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-96-72"); break;
case \$toplam_optik_noktasi <= 864 : \$hedefsecenek= array(
"96-96-96-96-96-96-96-96-96"); break;

}
``````

Now I am looking for a solution for Rack Cabinet