Search the Community
Showing results for tags 'optimization'.
-
Hello, Have an associative array of variable length and variable key names. The POST array can have the following structure, where [rec_n] can have one or many elements. : $_POST Array ( [rec_1769057] => on [rec_1768743] => on [ponumb] => D000000034 [strnbr] => 100 ) The 'n' is a variable SKU number and concatenated from a select statement. There are thousands of SKUs. PONUMB and STRNBR are static and will always be at the end of the array. I have an algorithm for slicing this array into two separate arrays. skuitems Array ( [rec_1769057] => on [rec_1768743] => on ) postrnbr Array ( [ponumb] => D000000034 [strnbr] => 100 ) The two arrays are then assigned to a $params array one for each update statement... $params Array ( [0] => Array ( [0] => 1768743 [1] => D000000034 [2] => 100 ) [1] => Array ( [0] => 1769057 [1] => D000000034 [2] => 100 )) I would like to accomplish the above in one go... At present the solution has and O(2) notation. Here is the algorithm that works, but seems clunky to me. if( isset($postar) && is_array($postar)) { // first order of mag. $cnt = count($postar) - 2; // total minus last two elements $postrnbr = array_slice( $postar, -2, 2 ); // always ponumb and strnbr $skuitems = array_slice($postar, 0, $cnt); // will be one or more sku items $skukeys = array_keys($skuitems); $qparams = array(); //bind params for update statement foreach($skukeys as $sku) { // second order of mag. $skusplit = explode("_",$sku); // ie Array( [0]=>rec, [1]=>1234545 ) $qparams[] = array($skusplit[1], $postrnbr['ponumb'], $postrnbr['strnbr']); } } Thanks in advance! rwhite35
-
Q: Should instancing an object be held off until needed - or - instanced early and held in memory until (it may/or may not) be called? Here is an example of the type situation where an custom error reporting class is instanced only when some test has failed. The error reporting is for authorized users running an application from protected sub sections(RESTish solution). /* * check form input and $_FILES upload * if test fails, instance errorwrapper::factory object and call errorReport method * testfilesize function, compares file size limit, set pass1 to true on success * testfiletype function, compares file to allowed MIME types, set pass2 to true on success * svalidate returns array $filteres, sanitized input on success, error message(s) on fail * param boolean $pass[1,2], set initial test condition * param int MAX_FILE_SIZE, constant defines max file size allowed. */ $pass1 = false; $pass2 = false; if(isset($_FILES)) { $ftype = $_FILES['img_file']['type']; $fsize = $_FILES['img_file']['size']; testfilesize($pass1,MAX_FILE_SIZE,$fsize); testfiletype($pass2,$ftype); } if($pass1 == false) { $erparams=array('cat'=>3,'src'=>"addform_control",'ref'=>"addform",'fault'=>"over file size limit"); $errObj=errorwrapper::factory($erparams); $errObj->errorReport(); exit(); } elseif ($pass2 == false) { $erparams=array('cat'=>3,'src'=>"addform_control",'ref'=>"addform",'fault'=>"illegal file type"); $errObj=errorwrapper::factory($erparams); $errObj->errorReport(); exit(); } /* * process form input, all fields checked before returning filteres * could report multiple errors, mode sets level of sanitization. */ $filteres = svalidate($_POST,$mode=1); if($filteres['exit']==1) { foreach($filteres['err'] as $err) { $mes .= $err."\n"; } $erparams=array('cat'=>2,'src'=>"addfo",'ref'=>"list_viewer",'fault'=>$mes); $errObj=errorwrapper::factory($erparams); $errObj->errorReport(); exit(); } else { //continue processing here, error checking over } In this case, the object $errObj is only instanced on a failed condition. However, $errObj could be instantiated before the conditional test and only calls the method when there is a failed condition. It seems like "six to one half dozen" but these applications are large and complex, so I'm looking to squeeze out any worthwhile optimization. Thanks for considering the question.