duanjian4698 2018-05-07 16:29
浏览 47
已采纳

php循环拉json数据字段

Hello everyone and thanks for your time. I am trying to set up a loop to pull specific fields from a json api feed. The json feed can be seen here:

https://uysmiami.com/search/api_server_custom.php

I am trying to set up a loop to grab data from specific fields from the feed but with my limited php am grabbing other stuff as well. The results of my loop are can be seen at:

http://uysmiami.com/search/stevetest.php

the code I'm using is:

<?php
$json = file_get_contents('https://api.iyba.pro/vessel?key=95d30b94b3a7e8b8c2df379fb00816960f86e203&id=80393');
$data = json_decode($json,true );
foreach($data as $vdata)
{  foreach($vdata as $number){
echo "Name: ".$number['ListingOwnerName']."<br />";
} }
?>

how can I pull the fields I want and not the first two and last 4 lines of meaningless stuff?

Thanks again.

  • 写回答

1条回答 默认 最新

  • douzi2333 2018-05-07 17:12
    关注

    I got your desired results doing something like this:

    $dataFromUrl = file_get_contents( 'https://uysmiami.com/search/api_server_custom.php' );
    $dataEn = json_decode( $dataFromUrl, TRUE );
    foreach( $dataEn['V-Data'] as  $vData )
    {
        if ( isset( $vData['ListingOwnerName'] ) )
        {
            echo "Name: " . $vData['ListingOwnerName'] . "<br />";
        }
    }
    

    I am not sure if you need to loop through more things which is why you nested another loop, but for the V-Data array from the API shown, that got all the names without any numbers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了