循环复杂的多维数组

时间:2017-09-09 13:09:09

标签: php multidimensional-array

我正在尝试从Neutronix API的一系列营养结果中提取所有变量。

阵列如下:
食物数组

Array ( [foods] => 
    Array ( [0] => Array ( 
            [food_name] => kale 
            [brand_name] => 
            [serving_qty] => 1 
            [serving_unit] => cup, chopped 
            [serving_weight_grams] => 130 
            [nf_calories] => 36.4 
            [nf_total_fat] => 0.52 
            [nf_saturated_fat] => 0.07 
            [nf_cholesterol] => 0 
            [nf_sodium] => 29.9 
            [nf_total_carbohydrate] => 7.32 
            [nf_dietary_fiber] => 2.6 
            [nf_sugars] => 1.63 
            [nf_protein] => 2.47 
            [nf_potassium] => 296.4 
            [nf_p] => 36.4 
            [full_nutrients] => Array ( 
                [0] => Array ( 
                    [attr_id] => 203 
                    [value] => 2.47 
                ) 
                [1] => Array ( 
                    [attr_id] => 204 
                    [value] => 0.52 
                ) 
                [2] => Array ( 
                    [attr_id] => 205 
                    [value] => 7.319 
                ) 
                [3] => Array ( 
                    [attr_id] => 207 
                    [value] => 1.131 
                ) 
                [4] => Array ( 
                    [attr_id] => 208 
                    [value] => 36.4 
                ) 
                [5] => Array ( 
                    [attr_id] => 221 
                    [value] => 0 
                ) 
                [6] => Array ( 
                    [attr_id] => 255 
                    [value] => 118.56 
                ) 
                [7] => Array ( 
                    [attr_id] => 262 
                    [value] => 0 
                ) 
                [8] => Array ( 
                    [attr_id] => 263 
                    [value] => 0 
                ) 
                [9] => Array ( 
                    [attr_id] => 268 
                    [value] => 152.1 
                ) 
                [10] => Array ( 
                    [attr_id] => 269 
                    [value] => 1.625 
                ) 
                [11] => Array ( 
                    [attr_id] => 291 
                    [value] => 2.6 
                ) 
                [12] => Array ( 
                    [attr_id] => 301 
                    [value] => 93.6 
                ) 
                [13] => Array ( 
                    [attr_id] => 303 
                    [value] => 1.17 
                ) 
                [14] => Array ( 
                    [attr_id] => 304 
                    [value] => 23.4 
                ) 
                [15] => Array ( 
                    [attr_id] => 305 
                    [value] => 36.4 
                ) 
                [16] => Array ( 
                    [attr_id] => 306 
                    [value] => 296.4 
                ) 
                [17] => Array ( 
                    [attr_id] => 307 
                    [value] => 29.9 
                ) 
                [18] => Array ( 
                    [attr_id] => 309 
                    [value] => 0.312 
                ) 
                [19] => Array ( 
                    [attr_id] => 312 
                    [value] => 0.2028 
                ) 
                [20] => Array ( 
                    [attr_id] => 315 
                    [value] => 0.5408 
                ) 
                [21] => Array ( 
                    [attr_id] => 317 
                    [value] => 1.17 
                ) 

            ) 
            [nix_brand_name] => 
            [nix_brand_id] => 
            [nix_item_name] => 
            [nix_item_id] => 
            [upc] => 
            [consumed_at] => 2017-09-08T22:44:54+00:00 
            [metadata] => Array ( ) 
                [source] => 1 
                [ndb_no] => 11234 
                [tags] => Array ( 
                    [item] => kale 
                    [measure] => 
                    [quantity] => 1.0 
                    [tag_id] => 644 
                ) 
                [alt_measures] => Array ( 
                    [0] => Array ( 
                        [serving_weight] => 130 
                        [measure] => cup, chopped 
                        [seq] => 1 
                        [qty] => 1 ) 
                    [1] => Array ( 
                        [serving_weight] => 130 
                        [measure] => cup 
                        [seq] => 80 
                        [qty] => 1 ) 
                    [2] => Array ( 
                        [serving_weight] => 2.71 
                        [measure] => tsp 
                        [seq] => 101 
                        [qty] => 1 ) 
                    [3] => Array ( 
                        [serving_weight] => 8.13 
                        [measure] => tbsp 
                        [seq] => 102 
                        [qty] => 1 ) 
                ) 
                [lat] => 
                [lng] => 
                [meal_type] => 5 
                [photo] => Array ( 
                    [thumb] => https://d2xdmhkmkbyw75.cloudfront.net/644_thumb.jpg 
                    [highres] => https://d2xdmhkmkbyw75.cloudfront.net/644_highres.jpg 
                ) 
                [sub_recipe] => 
        )

    ) 
)

到目前为止我的PHP代码如下。我已经插入了评论,我对如何获取变量感到困惑。

我也不确定我是否正确循环数组的片段。

foreach ($foods as $food){
    foreach($food as $key => $val) {
    //get values for each variable

        foreach($full_nutrients as $nutrient){
              foreach($nutrient as $key => $val){
              //get values for each variable
              }
        }
        foreach($metadata as $meta){
           $source = $meta['source'];
           $ndb_no = $meta['ndb_no'];
           foreach($tags as $tag){
              $tag_item = $tag['item'];
              $tag_measure = $tag['measure'];
              $tag_quantity = $tag['quantity'];
              $tag_id = $tag['tag_id'];
           }
        }
        foreach($alt_measures as $alt_meaasure){
           foreach($alt_meaasure as $key => $val){
           //get alt_measures
           }
        }
        foreach($photo as $image){
            $image_thumb = $image['thumb'];
            $image_highres = $image['highres'];
        }
    }
}

0 个答案:

没有答案