PHP在数组中打印数组的所有对象

时间:2018-05-19 07:17:34

标签: php

我需要在[products]数组中打印[categories]数组的所有对象,但它只打印一个对象......

我尝试了与[products]数组相同的方式以及其他各种方式,但没有找到解决方案。如何在数组中打印数组的所有对象?

这是api:

        stdClass Object
    (
        [count] => 25
        [total_count] => 4578
        [current_page] => 1
        [per_page] => 25
        [pages] => 184
        [products] => Array
            (
                [0] => stdClass Object
                    (
                        [id] => 100038
                        [key] => 100038
                        [name] => Kit Pads Open BIC
                        [additional_name] => 
                        [stock] => stdClass Object
                            (
                                [level] => 0
                                [text] => derzeit nicht verfügbar
                                [class] => red
                                [quantity] => 0
                            )

                        [price] => 79
                        [display_price] => CHF 79.00
                        [cost_price] => 56
                        [sales_unit] => Stück
                        [images] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [file_name] => OpenBIC_Rear-HR.jpg
                                        [updated_at] => 2015-08-17T00:00:38.974
                                        [mini_url] => /wsshop/Catrade/thumbs/OpenBIC_Rear-HR.jpg
                                        [small_url] => /wsshop/Catrade/thumbs/190/OpenBIC_Rear-HR.jpg
                                        [original_url] => /wsshop/Catrade/pict/OpenBIC_Rear-HR.jpg
                                    )

                                [1] => stdClass Object
                                    (
                                        [file_name] => OpenBic09_3-4_HR.jpg
                                        [updated_at] => 2015-08-17T00:00:38.974
                                        [mini_url] => /wsshop/Catrade/thumbs/OpenBic09_3-4_HR.jpg
                                        [small_url] => /wsshop/Catrade/thumbs/190/OpenBic09_3-4_HR.jpg
                                        [original_url] => /wsshop/Catrade/pict/OpenBic09_3-4_HR.jpg
                                    )

                                [2] => stdClass Object
                                    (
                                        [file_name] => OpenBic09_side_HR.jpg
                                        [updated_at] => 2015-08-17T00:00:38.974
                                        [mini_url] => /wsshop/Catrade/thumbs/OpenBic09_side_HR.jpg
                                        [small_url] => /wsshop/Catrade/thumbs/190/OpenBic09_side_HR.jpg
                                        [original_url] => /wsshop/Catrade/pict/OpenBic09_side_HR.jpg
                                    )

                            )

                        [brand] => stdClass Object
                            (
                                [id] => 189
                                [name] => Bic Sport
                                [logo_file_name] => Logo_BIC_Sport.jpg
                                [logo_updated_at] => 2015-06-11T00:00:54.322
                                [logo_original_url] => /wsshop/Catrade/pict/Logo_BIC_Sport.jpg
                            )

                        [has_variants] => 
                        [has_options] => 
                        [order_allowed] => 1
                        [sku] => 100038
                        [barcode] => 3590091000388
                        [categories] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => 4
                                        [key] => 4
                                        [name] => BIC / SIC / OXBOW
                                        [description] => Fokussiert auf die Herstellung von Surfbretter, Kajaks, Segelboote für Kinder und seit 2009 auch Stand UP Paddle Boards produziert BIC bis heute in Europa.
                                        [image] => stdClass Object
                                            (
                                                [file_name] => Marke_Stimmungsbild_BIC.jpg
                                                [updated_at] => 2015-06-22T00:01:01.868
                                                [mini_url] => /wsshop/Catrade/thumbs/Marke_Stimmungsbild_BIC.jpg
                                                [small_url] => /wsshop/Catrade/thumbs/190/Marke_Stimmungsbild_BIC.jpg
                                                [original_url] => /wsshop/Catrade/pict/Marke_Stimmungsbild_BIC.jpg
                                            )

                                        [subcategory] => stdClass Object
                                            (
                                                [id] => 4.6
                                                [key] => 6
                                                [name] => Open BIC
                                                [description] => 
                                            )

                                    )

                            )
                )

...

这是一个代码段:

       echo "</pre>";
    //$max = $phpObjekt->pages; 

    //speichert alle Produkte
    $phpProducts = $phpObjekt->products;

    // Ausgabe aus einem Objekt (direkt)
    for ($i = 0; $i < count($phpProducts); $i++) {

            //Speicherung der Daten in Variabeln
            $productId = $phpProducts[$i]->id;
            $productKey = $phpProducts[$i]->key;
            $productName = $phpProducts[$i]->name;
            $productAdditional_name = $phpProducts[$i]->additional_name;
            $productText = $phpProducts[$i]->stock->text;
            $productPreis = $phpProducts[$i]->price;
            $productDisplay_price = $phpProducts[$i]->display_price;
            $productCost_price = $phpProducts[$i]->cost_price;
            $productSales_unit = $phpProducts[$i]->sales_unit;

...

1 个答案:

答案 0 :(得分:0)

使用foreach()你可以做类似的事情......

foreach ( $phpObjekt->products as $product )    {
    //Speicherung der Daten in Variabeln
    $productId = $product->id;
    // ...

    foreach ( $product->categories as $category )   {
        $categoryID = $category->id;
        // ..
    }
}