Re: Array Help Needed



Thank you for the help Aho.

// pull subscriptions
$subscriptions = array();

foreach ($order->products as $product)
{
foreach ($product['attributes'] as $attribute)
{
if ($attribute['option'] != 'Subscription')
continue 2;
$subscriptions[] = $product;
}
}

As a follow up question do you know how I could do the following:

Get all the [final_price] from the array that are from [attributes] [option]
== 'Subscription' and add them into a total sum.

Here is the print_r() for reference:

order Object
(
[info] => Array
(
[order_status] => 1
[currency] => USD
[currency_value] => 1.00000000
[payment_method] => Credit Card
[payment_module_code] => authorizenet
[coupon_code] =>
[shipping_method] => Flat Rate (Best Way)
[shipping_module_code] => flat_flat
[shipping_cost] => 5.00
[subtotal] => 36.84
[tax] => 0
[total] => 41.84
[tax_groups] => Array
(
[Sales Tax] => 0
)

[comments] => THIS IS A TEST ORDER #7
[ip_address] => 24.19.197.137 - 24.19.197.137
)

[totals] => Array
(
)

[products] => Array
(
[0] => Array
(
[qty] => 1
[name] => Change is Coming News Report (Subscription)
[model] => 4212-70-219
[tax] => 0
[tax_description] => Sales Tax
[price] => 5.9500
[final_price] => 5.95
[onetime_charges] => 0
[weight] => 0
[products_priced_by_attribute] => 0
[product_is_free] => 0
[products_discount_type] => 0
[products_discount_type_from] => 0
[id] => 73:f71dd7cced862b1ef518cdf9eaf0fdd5
[rowClass] => rowEven
[attributes] => Array
(
[0] => Array
(
[option] => Subscription
[value] => Yearly
[option_id] => 1
[value_id] => 1
[prefix] => +
[price] => 0.0000
)

)

)

[1] => Array
(
[qty] => 1
[name] => News Report (Subscription)
[model] =>
[tax] => 0
[tax_description] => Sales Tax
[price] => 10.9900
[final_price] => 10.99
[onetime_charges] => 0
[weight] => 0
[products_priced_by_attribute] => 0
[product_is_free] => 0
[products_discount_type] => 0
[products_discount_type_from] => 0
[id] => 185:b2e375e0be1b2613924a38d29754c259
[rowClass] => rowOdd
[attributes] => Array
(
[0] => Array
(
[option] => Subscription
[value] => Monthly
[option_id] => 4
[value_id] => 5
[prefix] => +
[price] => 0.0000
)

)

)

[2] => Array
(
[qty] => 1
[name] => A Guide to South American Culture
[model] => 4200-70-60
[tax] => 0
[tax_description] => Sales Tax
[price] => 9.9500
[final_price] => 9.95
[onetime_charges] => 0
[weight] => 1
[products_priced_by_attribute] => 0
[product_is_free] => 0
[products_discount_type] => 0
[products_discount_type_from] => 0
[id] => 6
[rowClass] => rowEven
)

[3] => Array
(
[qty] => 1
[name] => Building Values Within Society
[model] => 4200-70-64
[tax] => 0
[tax_description] => Sales Tax
[price] => 9.9500
[final_price] => 9.95
[onetime_charges] => 0
[weight] => 1
[products_priced_by_attribute] => 0
[product_is_free] => 0
[products_discount_type] => 0
[products_discount_type_from] => 0
[id] => 3
[rowClass] => rowOdd
)

)

[customer] => Array
(
[firstname] => John
[lastname] => Doe
[company] =>
[street_address] => 123 4th Street
[suburb] =>
[city] => Seattle
[postcode] => 98059
[state] => Washington
[zone_id] => 62
[country] => Array
(
[id] => 223
[title] => United States
[iso_code_2] => US
[iso_code_3] => USA
)

[format_id] => 2
[telephone] => 222 333-4444
[email_address] => qwerty@xxxxxxxx
)

[delivery] => Array
(
[firstname] => John
[lastname] => Doe
[company] =>
[street_address] => 123 4th Street
[suburb] =>
[city] => Seattle
[postcode] => 98059
[state] => Washington
[zone_id] => 62
[country] => Array
(
[id] => 223
[title] => United States
[iso_code_2] => US
[iso_code_3] => USA
)

[country_id] => 223
[format_id] => 2
)

[content_type] => physical
[email_low_stock] =>
[products_ordered_attributes] =>
[products_ordered] =>
[products_ordered_email] =>
[attachArray] =>
[observers] => Array
(
)

[billing] => Array
(
[firstname] => John
[lastname] => Doe
[company] =>
[street_address] => 123 4th Street
[suburb] =>
[city] => Seattle
[postcode] => 98059
[state] => Washington
[zone_id] => 62
[country] => Array
(
[id] => 223
[title] => United States
[iso_code_2] => US
[iso_code_3] => USA
)

[country_id] => 223
[format_id] => 2
)

)


"J.O. Aho" <user@xxxxxxxxxxx> wrote in message
news:6nomlkFfpdU1@xxxxxxxxxxxxxxxxxxxxx
Jerry wrote:
Hi,

I have an array of ORDER DETAILS (see below). I would like to pull out of
the order Object array all the [products] in the order that have the
[attributes] [option] equal to 'Subscription' (this example the order has
2
subscription products and 2 products that are not subscriptions, but
other
orders may have more or less), and put them into a new array with the
following structure:


foreach($object->products as $product)
foreach($product[attributes] as $att)
if($att[option] == 'Subscription')
echo $product[name];

See http://www.php.net/foreach

You may want to make checks that you have arrays, as otherwise you may get
a
not that nice surprise when an array hasn't been set.

--

//Aho



.



Relevant Pages

  • Re: Array Help Needed
    ... // pull subscriptions ... foreach { ... you could skip that array completely in your design. ...
    (alt.php)
  • Re: Array Help Needed
    ... // pull subscriptions ... foreach { ... you could skip that array completely in your design. ...
    (alt.php)
  • Re: Array Help Desired
    ... foreach ... 'Subscription' and add them into a total sum. ... use conditions to add to the total price when appropriate. ...
    (comp.lang.php)
  • old sysmergesubscriptions records
    ... We have a replication system with hundreds of merge pull subscriptions ... that have appeared in the sysmergesubscriptions tables all over the place. ... The push subscriptions are all subscribing from and to the same server name, ... It also has two local pull subscriptions to TESTNET-SERVER which i want to ...
    (microsoft.public.sqlserver.programming)
  • Re: Array Help Desired
    ... foreach ... Sorry, Jerry. ... Hope it got you started on the right foot. ...
    (comp.lang.php)