【问题标题】:Foreach to get data from array in wordpress phpForeach从wordpress php中的数组获取数据
【发布时间】:2016-02-14 15:12:23
【问题描述】:

大家好,当我执行 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

我用这个功能得到了这个

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);

如何再次执行 foreach 循环以获取变体属性?在给定的产品 id 9_97_98_76 中,产品 id 97 和 98 是 76 的变体产品。

我想得到产品的标题和变量属性,我如何编码foreach,使结果返回如下数组:test_test222222 white_test222222 black_test222222 ???

【问题讨论】:

  • 嗯,你最终想做什么?这个数组是从哪里来的?如果您想查询某些产品,为什么不使用WP_Query
  • 同意@helgatheviking - 如果有办法使用 WordPress 功能做到这一点,你应该这样做。

标签: php arrays wordpress foreach woocommerce


【解决方案1】:

试试这个会得到你的标题

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

【讨论】:

    【解决方案2】:

    试试这个。希望您使用get_product() 函数获得productID 的产品对象。然后尝试array_push 将所有过滤后的产品对象插入到 1 个数组中。

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

    现在试试下面的

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

    【讨论】:

    • 这样吗?我尝试第 n 次发生 $product = Array(); foreach($pId as $productID){ array_push($product, $Pproduct->get_product($productID, $fields)); foreach($product as $temp) { $ii = $temp[variations]; } } 返回 $ii
    • 是的.. 无论如何,您可以使用我之前提到的另一个 foreach 获取任何对象值。如果它对您有用,请对该答案投赞成票。以便对其他人有用。
    猜你喜欢
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 2014-09-24
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多