2016-04-15 156 views
0

我通过一个多维数组试图循环,但在foreach循环中,它只是输出错误遍历PHP多维数组

指数“名”未找到。索引 '卡路里' 不创始人

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

响应:即$ responsex

阵列( 'encodedId'=> '4H8xxx', '的displayName'=> 'SAM',)阵列(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”] => str (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 (6){[“calories”] => int(574)[“carbs”] => float(49.16)[“fat”)[7] => int(389)}} [“nutritionalValues”] => “]> => 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”] => (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, 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){[“calories”] => int(359)[“碳水化合物”)[6] => int(147) [7] => int(389)}} [“nutritionValues” ] => 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”] =>数组(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) ] =“0”)=> int(3)}}} [“goals”] => array(2){“calories”] => int(1161)[“estimatedCaloriesOut”] => int(1411) “=>数组(7){[”calories“] => int(1090) [”carbs“] => float(85.73)[”fat“] => float(61.46)[”fiber“] => float(7.4) [“protein”] => float(50。35)[ “钠”] => INT(2061)[ “水”] => INT(0)} }

+0

您可以为我们使用的3v4l.org。你的阵列很大,可以用来调试。 –

+0

使用print_r或json_encode显示结果 – dev

回答

1

就可以通过阵列递归迭代并打印它们作为关键值对如下。

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

除了使用上面的代码打印它们之外,您可以定义其他任务。

编辑

,如果你确信该阵列是二维的,没必要去递归。

<?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); 
     } 
    } 
} 
+0

这只是给我内部数组“食物”的名称。它输出Array:食物。我需要访问“食物”数组中的值 – condo1234

+0

当在内部foreach中输出echo($ v)时,只返回一个数组。当我var_dump基本上是在“食物”索引中的数组初始响应 – condo1234

+0

啊,是的,你可以使用var_dump并一次获得整个数组。 –

0

用这种方式..

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

?>