rwhite35 Posted February 13, 2016 Share Posted February 13, 2016 (edited) 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 Edited February 13, 2016 by rwhite35 Quote Link to comment https://forums.phpfreaks.com/topic/300800-split-array-of-variable-length/ Share on other sites More sharing options...
Solution mac_gyver Posted February 13, 2016 Solution Share Posted February 13, 2016 you would use a html array name for the rec form field, where the array key is the SKU number - name='rec[1769057]' this will result in an array in $_POST['rec'] that you can use php's array functions on, such as a foreach(){} loop to loop over.the elements when providing data to your sql query. Quote Link to comment https://forums.phpfreaks.com/topic/300800-split-array-of-variable-length/#findComment-1531065 Share on other sites More sharing options...
rwhite35 Posted February 13, 2016 Author Share Posted February 13, 2016 Thanks mac_gyver, Ill try that and post the result. Quote Link to comment https://forums.phpfreaks.com/topic/300800-split-array-of-variable-length/#findComment-1531067 Share on other sites More sharing options...
Jacques1 Posted February 13, 2016 Share Posted February 13, 2016 (edited) Why not simply have one PONUMB, one STRNBR and a list of SKUs? <?php $_POST = [ 'ponumb' => 'D000000034', 'strnbr' => '100', 'skus' => [ '1769057', '1768743', ] ]; foreach ($_POST['skus'] as $sku) { $update_data = [ 'sku' => $sku, 'ponumb' => $_POST['ponumb'], 'strnbr' => $_POST['strnbr'], ]; var_dump($update_data); } To get a numerical array of SKUs, use the name="skus[]" syntax. Edited February 13, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/300800-split-array-of-variable-length/#findComment-1531068 Share on other sites More sharing options...
rwhite35 Posted February 13, 2016 Author Share Posted February 13, 2016 Hey Jacques, Thank you for the reply. I've coded up mac_gyvers answer and it works great. I think it is similar to what you are suggesting too. Later, Ron Quote Link to comment https://forums.phpfreaks.com/topic/300800-split-array-of-variable-length/#findComment-1531070 Share on other sites More sharing options...
rwhite35 Posted February 13, 2016 Author Share Posted February 13, 2016 This works great. you would use a html array name for the rec form field, where the array key is the SKU number - name='rec[1769057]' this will result in an array in $_POST['rec'] that you can use php's array functions on, such as a foreach(){} loop to loop over.the elements when providing data to your sql query. <?php $qparams = array(); if($_POST) { $skus = $_POST['rec']; foreach($skus as $key=>$value) { $qparams[] = array($key, $_POST['ponumb'], $_POST['strnbr']); } } echo "<pre>"; print_r($qparams); echo "</pre>"; ?> <html> <head> <title>Testing HTML Array</title> </head> <body> <form action="", method=post> <fieldset><label>Test Form</label><br> 1769057 <input type="radio" name=rec[1769057] selected> 1768743 <input type="radio" name=rec[1768743] selected> <input type="text" name="ponumb" value="D000000034"> <input type="text" name="strnbr" value="100"><br> <input type="submit" value="submit"><br> </fieldset> </form> </body> </html> outputs Array ( [0] => Array ( [0] => 1769057 [1] => D000000034 [2] => 100 ) [1] => Array ( [0] => 1768743 [1] => D000000034 [2] => 100 )) Thanks! rwhite35 Quote Link to comment https://forums.phpfreaks.com/topic/300800-split-array-of-variable-length/#findComment-1531071 Share on other sites More sharing options...
Jacques1 Posted February 13, 2016 Share Posted February 13, 2016 The only reason for using the SKU as a key would be to store a corresponding value (or implement a hash set). Since you just want a list of SKUs, the above approach makes no sense. Yes, this kinda sorta “works”, in the same way that your array_slice() hack kinda sorta “worked”. But it makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/300800-split-array-of-variable-length/#findComment-1531074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.