0

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.

1
  • I couldn't understand why can't foreach ( $items as $item ) be changed to foreach ( $items as $k => $item )?
    – rakeshjain
    Mar 21, 2014 at 19:43

3 Answers 3

2

Just change the foreach syntax to be foreach($items as $key => $item). This would give you access to $key while not impacting any existing code in the loop which relies on $item.

5
  • I tried that, but there are values that are retrieved with $item['foo']. I tried setting it to $v['foo'], but it broke.
    – Plummer
    Mar 21, 2014 at 19:46
  • @tPlummer Why would you change to $v['foo'] if in your foreach you still have $key => $item? There should be no need at all to change any variable names inside the loop. Perhaps you should add more code context in your question and show specifically where you have such a problem.
    – Mike Brant
    Mar 21, 2014 at 20:01
  • Because The foreach is `foreach($order_items as $item): ... for( $x=1; $x <= $item['qty']; $x++ )
    – Plummer
    Mar 21, 2014 at 20:05
  • 1
    @tPlummer Yes. I am suggesting you change that line to foreach($order_items as $key => $item) that would give you access to the key for each item inside the loop.
    – Mike Brant
    Mar 21, 2014 at 20:07
  • Got it. Issue wasn't with my code by some where else when they referenced the param. Thanks bud.
    – Plummer
    Mar 21, 2014 at 20:20
0

Here's a quick and dirty approach. Would be expensive if you have a large array -- but otherwise it would work.

$items = $order->get_items();
array_walk($items, function(&$v, $i) { $v['id'] = $i; });

This code adds another key to the array called id and assigns it the current key value.

Requires PHP 5.4 +

Example

$arr = array(
    486 => array(
        'name' => 'Bob',
    ),
    500 => array(
        'name' => 'Sam',
    ),
);

array_walk($arr, function(&$v, $i) { $v['id'] = $i; });

print_r($arr);

Example Result

Array ( 
    [486] => Array ( 
        [name] => Bob 
        [id] => 486 
    ) 
    [500] => Array ( 
        [name] => Sam 
        [id] => 500 
    ) 
)
2
  • What is that $i? I seen it in other functions, but figured it was just a common parameter character.
    – Plummer
    Mar 21, 2014 at 19:59
  • In this example $i is the key of the current item being passed. function(&$value, $key) {} if you rename them. Mar 21, 2014 at 20:03
0

You can set the values in your array (if needed up the call stack)

I tried that, but there are values that are retrieved with $item['foo']. I tried setting it to $v['foo'], but it broke.

foreach( $items as $key => &$item ) {
    $item['id'] = $key;

    /* $item now looks like
     * [ 'id' => ...,
     *   ... ]
     */

} unset( $item ); //Needed otherwise references can become complicated
1
  • Can you expand on 'unset( $item ); //Needed otherwise references can become complicated' I dont think it makes much sense
    – RyanS
    Mar 21, 2014 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.