简体   繁体   中英

Foreach to get data from array in wordpress php

Hi guys i have this array when i do print_r($p)

Array
(
    [0] => Array
        (
            [product] => Array
                (
                    [title] => test
                    [id] => 9
                    [created_at] => 2015-08-11 19:32:05
                    [isNew] => 
                    [type] => simple
                    [status] => publish
                    [price] => 10.00
                    [regular_price] => 10.00
                    [sale_price] => 6.00
                    [stock_quantity] => 19999985
                    [featured] => 1
                    [on_sale] => 
                    [description] => 
                    [short_description] => 
                    [categories] => Array
                        (
                        )

                    [tags] => Array
                        (
                        )

                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [src] => 
                                )

                        )

                    [featured_src] => 
                    [attributes] => Array
                        (
                        )

                    [variations] => 
                )

        )

    [1] => Array
        (
            [product] => Array
                (
                    [title] => test222222
                    [id] => 97
                    [created_at] => 2015-08-31 17:40:54
                    [isNew] => 
                    [type] => variation
                    [status] => publish
                    [price] => 1
                    [regular_price] => 2
                    [sale_price] => 1
                    [stock_quantity] => 1999974
                    [featured] => 1
                    [on_sale] => 1
                    [description] => <p>tasdasd</p>

                    [short_description] => 
                    [categories] => Array
                        (
                        )

                    [tags] => Array
                        (
                        )

                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [src] => 
                                )

                        )

                    [featured_src] => 
                    [attributes] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Color
                                    [slug] => Color
                                    [position] => 0
                                    [visible] => 1
                                    [variation] => 1
                                    [options] => Array
                                        (
                                            [0] => black
                                            [1] => White
                                        )

                                )

                        )

                    [variations] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 98
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 199969
                                    [color] => black
                                )

                            [1] => Array
                                (
                                    [id] => 97
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 1999974
                                    [color] => White
                                )

                        )

                )

        )

    [2] => Array
        (
            [product] => Array
                (
                    [title] => test222222
                    [id] => 98
                    [created_at] => 2015-08-31 17:40:54
                    [isNew] => 
                    [type] => variation
                    [status] => publish
                    [price] => 1
                    [regular_price] => 2
                    [sale_price] => 1
                    [stock_quantity] => 199969
                    [featured] => 1
                    [on_sale] => 1
                    [description] => <p>tasdasd</p>

                    [short_description] => 
                    [categories] => Array
                        (
                        )

                    [tags] => Array
                        (
                        )

                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [src] => 
                                )

                        )

                    [featured_src] => 
                    [attributes] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Color
                                    [slug] => Color
                                    [position] => 0
                                    [visible] => 1
                                    [variation] => 1
                                    [options] => Array
                                        (
                                            [0] => black
                                            [1] => White
                                        )

                                )

                        )

                    [variations] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 98
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 199969
                                    [color] => black
                                )

                            [1] => Array
                                (
                                    [id] => 97
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 1999974
                                    [color] => White
                                )

                        )

                )

        )

    [3] => Array
        (
            [product] => Array
                (
                    [title] => test222222
                    [id] => 76
                    [created_at] => 2015-08-31 17:40:54
                    [isNew] => 
                    [type] => variable
                    [status] => publish
                    [price] => 0.00
                    [regular_price] => 0.00
                    [sale_price] => 0.00
                    [stock_quantity] => 50000
                    [featured] => 1
                    [on_sale] => 1
                    [description] => <p>tasdasd</p>

                    [short_description] => 
                    [categories] => Array
                        (
                        )

                    [tags] => Array
                        (
                        )

                    [images] => Array
                        (
                            [0] => Array
                                (
                                    [src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
                                )

                        )

                    [featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
                    [attributes] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Color
                                    [slug] => Color
                                    [position] => 0
                                    [visible] => 1
                                    [variation] => 1
                                    [options] => Array
                                        (
                                            [0] => black
                                            [1] => White
                                        )

                                )

                        )

                    [variations] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 98
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 199969
                                    [color] => black
                                )

                            [1] => Array
                                (
                                    [id] => 97
                                    [price] => 1
                                    [regular_price] => 2
                                    [stock] => 1999974
                                    [color] => White
                                )

                        )

                )

        )

)
null

i get this with this function

public function test(){
            global $wpdb;
            global $Pproduct;
            global $woocommerce;

            $productIds = 9_97_98_76;
            $pId = explode("_", $productIds);
            foreach($pId as $productID){
                $product[] = $Pproduct->get_product($productID, $fields);
                $p = $product;

            }
            print_r($p);

how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.

I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???

try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.

$product = Array();
 foreach($pId as $productID){
                array_push($product, $Pproduct->get_product($productID, $fields));

            }

now try the following

    foreach($product as $temp) {
        echo $temp[variations];
   }

Try this will get your title

$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM