Splitting array into two based on key value of that array

When our company sends a quote to a customer, we store the item as one row, with multiple quantities [i.e., the qty field would contain 2].

When they place an order, I need to take that line array and essentially create two arrays, or split that row into however many arrays the quantity value is.

My first step is looping through the lines that we have quoted the customer.  a print_r of that loop is below:

Array
(
    [qty_quoted] => 2
    [make_model] => JDN 05TS
    [accessory_details] =>
    [accessory_total] => 213.53
    [original_price] => 175.00
    [addl_discount] => 0.00
    [disc_price] => 724.56
    [product_details] => .5T WLL AIR CHAIN HOIST W/ 25' HOL
MAKE/MODEL: JDN 05TS~ CAPACITY: .5 TON ~ WEIGHT: 46.2 LBS. ~ MIN. HEAD ROOM: 15.5\" ~ HEIGHT OF LIFT: 10\' ~ CFM REQUIRED: 50

    [cost_plus_accessories] => 362.28
)

Array
(
    [qty_quoted] => 1
    [make_model] => JDN 1TS
    [accessory_details] =>
    [accessory_total] => 174.23
    [original_price] => 30.00
    [addl_discount] => 0.00
    [disc_price] => 199.73
    [product_details] => 1T WLL AIR CHAIN HOIST W/ 50' HOL
MAKE/MODEL: JDN 1TS~ CAPACITY: 1 TON ~ WEIGHT: 53 LBS. ~ MIN. HEAD ROOM: 16.1\" ~ HEIGHT OF LIFT: 10\' ~ CFM REQUIRED: 50

    [cost_plus_accessories] => 199.73
)

Notice that first array has a quantity of 2.  I need to split that array into two arrays, or create a new array with three keys each containing a quantity.
LVL 1
t3chguyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Needs a few lines of code, no direct way to do this.  I'll show you an example as soon as I have a test case set up.

What about the other elements of the array?  Are there any rules that would change those values?
Ray PaseurCommented:
Here's a partial solution.   I'll tackle the array of arrays next.
http://iconoun.com/demo/temp_t3chguy.php
<?php // demo/temp_t3chguy.php
/**
 * http://www.experts-exchange.com/questions/28698807/PHP-Strip.html
 */
error_reporting(E_ALL);
echo '<pre>';


$original = Array
( 'qty_quoted'            => '2'
, 'make_model'            => 'JDN 05TS'
, 'accessory_details'     => NULL
, 'accessory_total'       => '213.53'
, 'original_price'        => '175.00'
, 'addl_discount'         => '0.00'
, 'disc_price'            => '724.56'
, 'product_details'       => ".5T WLL AIR CHAIN HOIST W/ 25' HOL MAKE/MODEL: JDN 05TS~ CAPACITY: .5 TON ~ WEIGHT: 46.2 LBS. ~ MIN. HEAD ROOM: 15.5\" ~ HEIGHT OF LIFT: 10\' ~ CFM REQUIRED: 50"
, 'cost_plus_accessories' => '362.28'
)
;

// EXPAND THE ARRAY INTO THIS VARIABLE
$new = array();

// SAVE THE ORIGINAL COUNT
$cnt = $original['qty_quoted'];

// SET THE COUNT THAT WE WANT TO USE
$original['qty_quoted'] = 1;

// PROPAGATE ROWS OF THE ARRAY
while ($cnt)
{
    $new[] = $original;
    $cnt--;
}

// SHOW THE WORK PRODUCT
print_r($new);

Open in new window

t3chguyAuthor Commented:
I'm trying to get you some code, by creating arrays based on my data, if you still need.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Ray PaseurCommented:
This seems to work out right.
<?php // demo/temp_t3chguy.php
/**
 * http://www.experts-exchange.com/questions/28698807/PHP-Strip.html
 */
error_reporting(E_ALL);
echo '<pre>';


$original = [
[ 'qty_quoted'            => '2'
, 'make_model'            => 'JDN 05TS'
, 'accessory_details'     => NULL
, 'accessory_total'       => '213.53'
, 'original_price'        => '175.00'
, 'addl_discount'         => '0.00'
, 'disc_price'            => '724.56'
, 'product_details'       => ".5T WLL AIR CHAIN HOIST W/ 25' HOL MAKE/MODEL: JDN 05TS~ CAPACITY: .5 TON ~ WEIGHT: 46.2 LBS. ~ MIN. HEAD ROOM: 15.5\" ~ HEIGHT OF LIFT: 10\' ~ CFM REQUIRED: 50"
, 'cost_plus_accessories' => '362.28'
]
,
[ 'qty_quoted'            => '1'
, 'make_model'            => 'JDN 1TS'
, 'accessory_details'     => NULL
, 'accessory_total'       => '174.23'
, 'original_price'        => '30.00'
, 'addl_discount'         => '0.00'
, 'disc_price'            => '199.73'
, 'product_details'       => "1T WLL AIR CHAIN HOIST W/ 25' HOL MAKE/MODEL: JDN 1TS~ CAPACITY: 1 TON ~ WEIGHT: 53 LBS. ~ MIN. HEAD ROOM: 16.1\" ~ HEIGHT OF LIFT: 10\' ~ CFM REQUIRED: 50"
, 'cost_plus_accessories' => '199.73'
]
]
;

$new = [];
foreach ($original as $row)
{
    // EXPAND TO MULTIPLE ROWS IF NEEDED
    if ($row['qty_quoted'] > 1)
    {
        $rows = expand_the_row($row);
        foreach ($rows as $new_row)
        {
            $new[] = $new_row;
        }
    }
    // DO NOT EXPAND IF NOT NEEDED
    else
    {
        $new[] = $row;
    }
}

// SHOW THE WORK PRODUCT
var_dump($new);

function expand_the_row($original, $key = 'qty_quoted')
{
	$new = array();
	$cnt = $original[$key];
	$original[$key] = 1;
	while ($cnt)
	{
	    $new[] = $original;
	    $cnt--;
	}
    return $new;
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Just a note for going forward, instead of posting the output of print_r() you can use the Code snippet and post the output of var_export().  That way we can cut-and-paste to get an accurate representation of your test data.  Makes for faster and more accurate solutions!
t3chguyAuthor Commented:
This is awesome! Thanks for yet another amazing, and speedy solution!!!
Ray PaseurCommented:
Thanks for the points and thanks for using E-E.  See you next time, ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.