最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - how to handle multiple forloop?

matteradmin10PV0评论

I have and order_id as array but wc_get_order accepts only one id at a time how to loop through order_id and let the list of item id and item name? i am using this inside a class. it works fine if single id is passed as shown below

 public function getStuffDone()
            {
                order_id =array(358,368)

                $order = wc_get_order(358);//only single id is passed here
                $items = $order->get_items();
                foreach ($order->get_items() as $item_key => $item_values):
                    $item_id      = $item_values->get_id();
                    $item_name    = $item_values->get_name();
                endforeach;
                return $item_name;
            }

please help .thanks

I have and order_id as array but wc_get_order accepts only one id at a time how to loop through order_id and let the list of item id and item name? i am using this inside a class. it works fine if single id is passed as shown below

 public function getStuffDone()
            {
                order_id =array(358,368)

                $order = wc_get_order(358);//only single id is passed here
                $items = $order->get_items();
                foreach ($order->get_items() as $item_key => $item_values):
                    $item_id      = $item_values->get_id();
                    $item_name    = $item_values->get_name();
                endforeach;
                return $item_name;
            }

please help .thanks

Share Improve this question asked Oct 17, 2018 at 12:31 user145078user145078
Add a comment  | 

1 Answer 1

Reset to default 0

You can do something like this

public function getStuffDone() {

    $order_ids = array( 358,368 );

    $items = array(); // contains the names

    foreach ( $order_ids as $order_id ) {

        $order = wc_get_order( $order_id );

        foreach ( $order->get_items() as $item_values ) {

            $items[ $order_id ][] = $item_values->get_name(); // add the name to the array
        }
    }

    return $items; // array containing all the names
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far