gpt4 book ai didi

php - JSON fatal error foreach

转载 作者:行者123 更新时间:2023-11-28 02:09:52 25 4
gpt4 key购买 nike

我有以下内容:

include('php/get_recipe_byID.php');
$jsonstring1 = $recipe_byidarr;
$recip = json_decode($recipe_byidarr, true);

print_r($recip);

foreach ($recip['Data']['Recipes'] as $key => $newrecipe) {
// echo '<li>
// <a href="/recipe_search.php?id=' . $recipe['ID'] . '">';
echo 'seas';
echo $newrecipe['TITLE'];
echo '<br><br>';
}

当我在浏览器中调用它时,它告诉我

Fatal error: Cannot use string offset as an array in /var/www/recipe_search.php on line 43

这是 foreach 循环的行。

$recip 如下:

{"Data":{"Recipes":{"Recipe_9":{"ID":"9","TITLE":"Schnitzel","TEXT":"Alex\u00b4s Hausmannskost","COUNT_PERSONS":"4","DURATION":"40","USER_ID":"1","DATE":"2011-09-16 00:00:00"}}},"Message":null,"Code":200}

有人知道我的错误在哪里吗?

最佳答案

试一试:

$taskSeries = $array['Data']['Recipes']['Recipe_'.$_GET['id']];
if(array_key_exists('TITLE', $taskSeries)) {
$taskSeries = array($taskSeries);
}
foreach($taskSeries as $task) {
$title = $task['TITLE'];
// do something with $title and other
}

关于php - JSON fatal error foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9031555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com