Php 如何在json文件的第一个对象中限制foreach
您好,我正在尝试将json文件打印到表中。json文件来自此网站。我想打印$data->rates->periods->rates[0]->standard。但我明白了 无法将stdClass类型的对象用作 我的代码是:Php 如何在json文件的第一个对象中限制foreach,php,json,Php,Json,您好,我正在尝试将json文件打印到表中。json文件来自此网站。我想打印$data->rates->periods->rates[0]->standard。但我明白了 无法将stdClass类型的对象用作 我的代码是: $data = json_decode($response); echo '<table class="table"><thead><tr><th scope="col">Country</th><th sc
$data = json_decode($response);
echo '<table class="table"><thead><tr><th scope="col">Country</th><th scope="col">First</th><th scope="col">Last</th><th scope="col">Handle</th></tr></thead><tbody>';
foreach($data->rates as $rate){
echo '<tr><th scope="row">'.$rate->name.'</th>';
foreach($rate->periods as $period){
echo '<td>'.$period->rates->standard.'</td><td>'.$period->rates->redused.'</td><td>'.$period->rates->super_reduced.'</td></tr>';
}
}
echo '</tbody></table>';
我听到一个错误说
警告:中的字符串偏移量“name”非法
C:\xampp\htdocs\wordpress\wp content\plugins\test plug\functions.php
第23行
提前谢谢
$tarray = json_decode($sjson);
$newarray = array('sample'=>array());
foreach($tarray->name as $ix => $name) {
$newarray['catalog'][]=array(
'name' => $tarray->name[$ix],
'image' => $tarray->image[$ix],
'price' => $tarray->price[$ix],
);
}
$sjson = json_encode($newarray);
或者您可以使用.split您需要更改代码,如下所示:
$json = file_get_contents("https://jsonvat.com/");
$dataObject = json_decode($json, true);
echo '<table class="table"><thead><tr><th scope="col">Country</th><th scope="col">First</th><th scope="col">Last</th><th scope="col">Handle</th></tr></thead><tbody>';
foreach($dataObject['rates'] as $rate){
echo '<tr><th scope="row">'.$rate['name'].'</th>';
foreach($rate['periods'] as $period){
echo '<td>'.$period['rates']['standard'].'</td><td>'.$period['rates']['reduced'].'</td><td>'.$period['rates']['super_reduced'].'</td></tr>';
}
}
echo '</tbody></table>';
在这里,我发现在速率键中有时不存在减缩键或超减缩键。因此,您需要相应地更改代码。希望它能对您有所帮助。您在尝试访问输入错误时遇到了一些问题。并非所有元素都有一个reduced Not“redused”值,有些元素有reduced1和reduced2值,或者根本没有,与super_reduced相同。在代码中,我使用了空合并??如果找不到值,则设置默认值。你可能想要调整这些来显示一些你认为最好的东西,而不是显示任何价值
echo '<td>'.$period->rates->standard.'</td><td>'.
($period->rates->reduced??$period->rates->reduced1??'Non').
'</td><td>'.($period->rates->super_reduced??'Non').'</td></tr>';
您可以使用简单的foreach循环访问这些值,并根据需要对元素进行设置
演示:您误解了的第二个参数:doc告诉: 如果为TRUE,则返回的对象将转换为关联数组 因此,以前您应该使用$data->rates访问费率,现在您应该使用$data['rates']对$rate->periods使用相同的$data['rates'],这将成为$rate['periods'] 因此,您可能看起来像未经测试的 foreach$数据['rates']作为$rate{ echo.$rate['name']。; }
当您解码为一个对象时,嵌套是$data->rates[0]->periods[0]->rates->standard,其中第一个速率和周期是数组。只需将两者循环:
foreach($data->rates as $rates) {
foreach($rates->periods as $period) {
echo $period->rates->standard . "<br />\n";
echo $period->rates->reduced. "<br />\n";
echo $period->rates->super_reduced . "<br />\n";
}
}
如果某些属性不能保证存在,请使用isset进行检查。尝试此功能可以帮助您,或者您可以使用此on功能
<?php
$count = 0;
$limit = 50;
foreach($data['rates'] as $rate) {
//count times export data
$count++;//first = 0
if($count == $limit)
break;//if limit == count stop
//export data
echo $rate['name'];
}
?>
谢谢你的来信。我的问题不只是名称,而是我想一次打印整个json,但只有第一个对象。你能给我看一个json示例和响应要求吗?json链接在原始帖子中。我想打印国家的名称,并且只打印当前的利率。它是嵌套的,如so$data->rates[0]->periods[0]->rates->standard,其中第一个利率有多个对象。当我做回显时,这似乎不会打印任何内容。哦,好吧,我错了。如何在foreach中执行此操作?您只想打印文件中的所有标准费率?谢谢您的回复。这确实有效,但不限于[0]对象我不明白你的意思,它不限于[0]对象?你能解释一下吗?没关系。你的代码帮助我解决了我的问题。非常感谢。
foreach($data->rates as $rates) {
foreach($rates->periods as $period) {
echo $period->rates->standard . "<br />\n";
echo $period->rates->reduced. "<br />\n";
echo $period->rates->super_reduced . "<br />\n";
}
}
<?php
$count = 0;
$limit = 50;
foreach($data['rates'] as $rate) {
//count times export data
$count++;//first = 0
if($count == $limit)
break;//if limit == count stop
//export data
echo $rate['name'];
}
?>