0

I am trying to loop through a multidimensional array but in the foreach loop it just outputs error

index 'name' not found. index 'calories' not founder

        foreach($responsex['foods'] as $fx5)
        {
            echo($fx5['name']);
            echo($fx5['calories']);
        }

Response: i.e. $responsex

array ( 'encodedId' => '4H8xxx', 'displayName' => 'sam', )array(3) { ["foods"]=> array(3) { [0]=> array(5) { ["isFavorite"]=> bool(false) ["logDate"]=> string(10) "2016-04-15" ["logId"]=> int(7139364449) ["loggedFood"]=> array(10) { ["accessLevel"]=> string(6) "PUBLIC" ["amount"]=> int(2) ["brand"]=> string(0) "" ["calories"]=> int(574) ["foodId"]=> int(536497687) ["locale"]=> string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=> string(14) "Potato Pudding" ["unit"]=> array(3) { ["id"]=> int(91) ["name"]=> string(3) "cup" ["plural"]=> string(4) "cups" } ["units"]=> array(8) { [0]=> int(6754) [1]=> int(91) [2]=> int(256) [3]=> int(279) [4]=> int(226) [5]=> int(180) [6]=> int(147) [7]=> int(389) } } ["nutritionalValues"]=> array(6) { ["calories"]=> int(574) ["carbs"]=> float(49.16) ["fat"]=> float(34.98) ["fiber"]=> float(3.6) ["protein"]=> float(16.1) ["sodium"]=> int(1524) } } [1]=> array(5) { ["isFavorite"]=> bool(false) ["logDate"]=> string(10) "2016-04-15" ["logId"]=> int(7138517833) ["loggedFood"]=> array(10) { ["accessLevel"]=> string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=> string(0) "" ["calories"]=> int(359) ["foodId"]=> int(535239347) ["locale"]=> string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=> string(54) "Fish, Noodles and Vegetables in Cheese Sauce (Mixture)" ["unit"]=> array(3) { ["id"]=> int(91) ["name"]=> string(3) "cup" ["plural"]=> string(4) "cups" } ["units"]=> array(8) { [0]=> int(6837) [1]=> int(91) [2]=> int(256) [3]=> int(279) [4]=> int(226) [5]=> int(180) [6]=> int(147) [7]=> int(389) } } ["nutritionalValues"]=> array(6) { ["calories"]=> int(359) ["carbs"]=> float(28.01) ["fat"]=> float(14.05) ["fiber"]=> float(2.9) ["protein"]=> float(29.08) ["sodium"]=> int(534) } } [2]=> array(5) { ["isFavorite"]=> bool(false) ["logDate"]=> string(10) "2016-04-15" ["logId"]=> int(7138326866) ["loggedFood"]=> array(10) { ["accessLevel"]=> string(6) "PUBLIC" ["amount"]=> int(1) ["brand"]=> string(0) "" ["calories"]=> int(157) ["foodId"]=> int(536493638) ["locale"]=> string(5) "en_AU" ["mealTypeId"]=> int(7) ["name"]=> string(11) "Cashew Nuts" ["unit"]=> array(3) { ["id"]=> int(226) ["name"]=> string(2) "oz" ["plural"]=> string(2) "oz" } ["units"]=> array(4) { [0]=> int(226) [1]=> int(180) [2]=> int(147) [3]=> int(389) } } ["nutritionalValues"]=> array(6) { ["calories"]=> int(157) ["carbs"]=> float(8.56) ["fat"]=> float(12.43) ["fiber"]=> float(0.9) ["protein"]=> float(5.17) ["sodium"]=> int(3) } } } ["goals"]=> array(2) { ["calories"]=> int(1161) ["estimatedCaloriesOut"]=> int(1411) } ["summary"]=> array(7) { ["calories"]=> int(1090) ["carbs"]=> float(85.73) ["fat"]=> float(61.46) ["fiber"]=> float(7.4) ["protein"]=> float(50.35) ["sodium"]=> int(2061) ["water"]=> int(0) } }

2
  • Can you create an 3v4l.org for us to use. Your array is quite big to debug with. Commented Apr 15, 2016 at 10:03
  • plz show result using print_r or json_encode Commented Apr 15, 2016 at 10:08

2 Answers 2

1

you can recursively iterate through the arrays and print them as follows as key value pairs.

<?php 
//initially call the function
print_array($responsex);

function print_array($array){
    foreach($array as $key=>$value){
        //recursively print the array
        if(is_array($value)){
            echo("Array : ".$key."\n");
            print_array($value);
        }
        else{
            echo($key." => ".$value);
        }
    }
}
?>

You can define additional tasks other than printing them with the above code.

Edit:

if you are sure that the array is two dimensional, no need to go recursively.

<?php 
//initially call the function
print_array($responsex);

//if you are sure that the array is two dimensional, no need to go recursively.
function print_array($array){
    foreach($array as $key=>$value){
        if(is_array($value)){
            if($key==="foods"){
                var_dump($array[$key]);
            }
        }
        else{
            echo($key." => ".$value);
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

This just gives me the name of the inner array "foods". It outputs Array : foods. I need to access the values within the "foods" array
When outing echo ($v) in the inner foreach, just returns an array. When I var_dump its basically the array in the "food" index in initial response
Ahh, yes, you can use var_dump and get the whole array at once.
But I need to access the values inside the food array, not return the array. When I echo $(v) it gives me error "Array to string conversion"
Since you have arrays under 'foods' key. you need to go for a recursive method.
0

Use this way..

<?php

 $keys = array_keys($data);// put your array name as a place of $data
    $iterations = count($array[$keys[0]]);

    for($i = 0; $i < $iterations; $i++) {
        $data = array();
        foreach($array as $key => $value) {
            $data[$key] = $value[$i];
        }
        print_r($data);
    }

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.