Jump to content

Split Array of Variable Length


rwhite35
Go to solution Solved by mac_gyver,

Recommended Posts

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 by rwhite35
Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.