I'm working in WooCommerce, but my question is related to basic PHP foreach
statements.
I have a function that returns a multidimensional array. The topmost dimension, the key string is what I need. If you've familiar, $order->get_items();
.
The order_item_id
is stored as the key like this:
'488' => Array( /* Array stuffs */ )
That 488
is what I need. It wouldn't be a problem if the function I'm working with's foreach
statement was:
foreach ($items as $k => $v)
Where I could just use $k
, but it's set up as:
foreach ($items as $item)
Is there a way to set the value of the key in the array and pass it along as a parameter?
UPDATE:
Here is the function as it works.
foreach($order_items as $item) {
$product = $order->get_product_from_item($item);
$gc_enabled = get_post_meta($product->id, 'ignite_gift_enabled', true);
if ( ! $gc_enabled)
continue;
$coupon_prefix = get_post_meta($product->id, '_coupon_prefix', true);
if ( ! $coupon_prefix)
$coupon_prefix = '';
for ($x = 1; $x <= $item['qty']; $x++) {
$new_coupon = $this->adjust_voucher( $coupon_prefix, $mode, $msg_details, $order_id, $order_item_id, $product->id );
}
}
$order_items
is an array of items, the keys of which I need to pass along to other functions referenced, specificaly adjust_voucher
.