Jump to content

Creating neat little sub-array from big array


ChenXiu
Go to solution Solved by Barand,

Recommended Posts

My longest post of the year..... (thank you in advance for scrolling 😀)
Here is what my $_POST array looks like using print_r($_POST)

Array
(
    [newQuantity77777] => 3
    [newPrice77777] => 5.00
    [usedQuantity77777] => 1
    [usedPrice77777] => 3.99
    [total77777] => 18.99
    [newQuantity88888] => // sometimes empty
    [newPrice88888] =>
    [usedQuantity88888] => 4
    [usedPrice88888] => 12.00
    [total88888] => 48.00
    [newQuantity44444] => 2
    [newPrice44444] => 4.00
    [usedQuantity44444] => 0
    [usedPrice44444] => 3.99
    [total44444] => 8.00 // these values I don't need
    [date] => July 25 2021 // these values below I don't need
    [address] => 123 Anystreet Avenue
    [address2] =>
    [zipcode] => 90210
    [city] => Beverly Hills
    [state] => CA
    [planet] => Mars
)

I've been trying to use that array to create a special "sub-array" for only the SKU numbers and just their new and used quantities and prices.

DESIRED RESULT:

Array
(
    [77777] => Array
        (
            [newQuantity] => 3
            [newPrice] => 5.00
            [usedQuantity] => 1
            [usedPrice] => 3.99
        )

    [88888] => Array
        (
            [newQuantity] => 0
            [newPrice] => 0
            [usedQuantity] => 4
            [usedPrice] => 12.00
        )

    [44444] => Array
        (
            [newQuantity] => 2
            [newPrice] => 4.00
            [usedQuantity] => 0
            [usedPrice] => 3.99
        )

)

Knowing that my SKU numbers are always exactly 5 digits, and no other $_POST keys will ever have 5 digits, I've been able to accomplish this with horribly convoluted and unsatisfactory code like this:

$sku = array();
foreach($_POST as $var => $val) {
$number = substr($var,-5);
if (preg_match("/\d{5}/",$sku)) {
$sku[$number] = // the array keys will be the SKU numbers
// then I keep looping to look for the string "newQuantity"
// capture that value... and create little mini arrays
// for my big multidimensional array.....

Is there a better way to go about this?

Thank you.

Link to comment
Share on other sites

I know, I agree! But I can't. That posted array comes from a 3rd party.
Actually, not too long ago when you were helping me with another mySQL issue, I learned about how use create those mini-arrays with my POST data (at first I thought it was complicated, but here's a perfect example of why doing a little prep work in the beginning is an excellent idea).

Anyway, I'm stuck with receiving this type of an array. Over the past couple years, I throw about an hour or two a day at it to try to come up with something better -- I have several solutions that work..... but they all involve looping and either using strpos or preg_match. At the end of each daily 2-hour sessions I spend on this, I end up scrapping my work and reverting to what I have because, at least, my original work never triggers any errors 😃

If you can think of any ideas to point me in the right direction I would appreciate it. The only consistant thing I have to go on is the SKU numbers always are digits, are always 5 digits long, and nothing else in the $_POST variable ever has 5 consecutive digits. I cannot think of any other sure-fire way than to loop through each value, capture those 5 digits, and then find every post variable with the captured 5 digits to then capture the quantity and price.

(And, once I have that, the rest of my code is starting to shape up nicely).

Edited by ChenXiu
Link to comment
Share on other sites

  • Solution

I would ...

foreach ($array as $k => $v)  {
    $id = substr($k, -5);
    if (ctype_digit($id)) {
        $len = strlen($k);
        $k = substr($k, 0, $len-5);
        $sku[$id][$k] = $v;
    }
    else $sku[$k] = $v;
}
echo '<pre>' . print_r($sku, 1) . '</pre>';

giving...

Array
(
    [77777] => Array
        (
            [newQuantity] => 3
            [newPrice] => 5
            [usedQuantity] => 1
            [usedPrice] => 3.99
            [total] => 18.99
        )

    [88888] => Array
        (
            [newQuantity] => 0
            [newPrice] => 0
            [usedQuantity] => 4
            [usedPrice] => 12
            [total] => 48
        )

    [44444] => Array
        (
            [newQuantity] => 2
            [newPrice] => 4
            [usedQuantity] => 0
            [usedPrice] => 3.99
            [total] => 8
        )

    [date] => July 25 2021
    [address] => 123 Anystreet Avenue
    [address2] => 
    [zipcode] => 90210
    [city] => Beverly Hills
    [state] => CA
    [planet] => Mars
)

 

  • Like 1
Link to comment
Share on other sites

That is fantastic! I also like your change to line 6 -- it makes for clearer code.
These days I'm learning to make my code neater, and to use actual words (like your $sku['products']) so at a later time, I know what the heck my code means.
Years ago I thought it "saved space" and "made PHP faster" if I scrunched everything on one line!
(made-up example): $aaa=array($cz =>$w8);$c=$5;foreach($a as $b){if($b!=0){$czn = $rrr}} <--- OMG!

That works fine until 6 months later an "error on line 10" appears and not only do I not remember what any of the variables mean, I can't find the line -- because the entire code is all on one line haha LOL.

Anyway, thank you!!

Link to comment
Share on other sites

1 hour ago, ChenXiu said:

it makes for clearer code.

It also gives a better structure to the resulting array, to make those with numbered keys a sub-group

Array
(
    [products] => Array
        (
            [77777] => Array
                (
                    [newQuantity] => 3
                    [newPrice] => 5
                    [usedQuantity] => 1
                    [usedPrice] => 3.99
                    [total] => 18.99
                )

            [88888] => Array
                (
                    [newQuantity] => 0
                    [newPrice] => 0
                    [usedQuantity] => 4
                    [usedPrice] => 12
                    [total] => 48
                )

            [44444] => Array
                (
                    [newQuantity] => 2
                    [newPrice] => 4
                    [usedQuantity] => 0
                    [usedPrice] => 3.99
                    [total] => 8
                )

        )

    [date] => July 25 2021
    [address] => 123 Anystreet Avenue
    [address2] => 
    [zipcode] => 90210
    [city] => Beverly Hills
    [state] => CA
    [planet] => Mars
)

 

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.