t3chguy
asked on
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.
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.
Here's a partial solution. I'll tackle the array of arrays next.
http://iconoun.com/demo/temp_t3chguy.php
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);
ASKER
I'm trying to get you some code, by creating arrays based on my data, if you still need.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
ASKER
This is awesome! Thanks for yet another amazing, and speedy solution!!!
Thanks for the points and thanks for using E-E. See you next time, ~Ray
What about the other elements of the array? Are there any rules that would change those values?