I am trying to build a table of part numbers with this script. I am quickly running out of memory. I have commented out the loops I couldn't even get to:
foreach ($mat_codes as $mat_code) {
for ($n = 0; $n < count($shape_codes); $n++){
for ($d1 = 0; $d1 <= 12; $d1++){
for ($d1_dec = 1; $d1_dec <= 16; $d1_dec++){
for ($d2 = 0; $d2 <= 12; $d2++){
//for ($d2_dec = 1; $d2_dec <= 16; $d2_dec++){
$build_part = (object) NULL;
$build_part = $mat_code->part_code;
$build_part .= $shape_codes[$n]->part_code;
$build_part .= '.';
if ($d1 != 0) {
$build_part .= $d1 . '-';
}
$build_part .= $d1_dec;
$build_part .= '.';
// if ($d2 != 0) {
// $build_part .= $d2 . '-';
// }
// $build_part .= $d2_dec;
// $build_part .= '.';
// * save $build_part to MYSQL * //
//}
}
}
}
}
}
Unsurprisingly this is returning
PHP Fatal error: Allowed memory size of 134217728 byets exhauseted (tried to allocate 71 bytes)
For the record there are about 16 values in $mat_codes
and 16 in $shape_codes
. By my calculations, I'm trying to create 692,224 part numbers in this example alone. I think I'm going to add more and have up to ~34 million. IMHO, it would be silly to try and get this done by just allocating more memory to the job. My question -- of course -- how do I break this script up into smaller chunks?
Edit:
I have a workaround. I apologize it is out of the scope I defined in the original question. I didn't mention that this is simply a table of potential part numbers. Particularly, all the potential part numbers allowed by the logic of the part numbering system. Rather than have a master list of part numbers at the ready just waiting to be used I figure I might be better off validating new part input against the part numbering logic, a function like could_this_be_a_part_number($input)
. It could just test the inputted string against the part numbering logic as opposed to having some master table I would need to check against.
Edit x2: I'm sorry this is the worst. I moved unset($build_part)
into the innermost for loop. The problem was I was trying to a development function in Drupal to print out each $build_part before it was unset - dpm($build_part)
. The script runs fine, its trying to build a webpage with that many part numbers printed to it is what breaks the script.