dongsha7215 2014-09-16 17:43
浏览 23
已采纳

从PHP数据中获取信息

I have no idea even what this is called so had no idea what to search for. I do apologise if this already exists.

I have information that I can get from going to a specific URL. It displays with something that looks like this:

 {"loading":false,"playing":true,"position":5730,"duration":211875,"index":0,"repeat":false,"shuffle":false,"volume":1,"context":null,"contexts":[],"track":{"artists":[{"name":"R3hab","uri":"spotify:artist:6cEuCEZu7PAE9ZSzLLc2oQ"},{"name":"Vinai","uri":"spotify:artist:4mrBetqy378Jf1y6NLszlx"}],"disc":0,"duration":212000,"image":"spotify:image:9ab80608da23ad97a5fd1cd8285f7a9239169c9d","images":[[64,"spotify:image:544aab2c02ea3ece23145ac3fdb78649e1181cad"],[300,"spotify:image:9ab80608da23ad97a5fd1cd8285f7a9239169c9d"],[600,"spotify:image:8537cde9090567164f449b25c00ad0c40e5c7325"]],"name":"How We Party - Original Mix","number":1,"playable":true,"popularity":67,"starred":false,"explicit":false,"availability":"premium","album":{"uri":"spotify:album:2IhQHv3MS8iq26We33dLdQ"},"local":false,"advertisement":false,"placeholder":false,"uri":"spotify:track:2b0N6oW4f87wNr9uahicc2"}}

If you scroll half through you'll see:

"name":"How We Party - Original Mix

I need a PHP script that would find the name of the track and displays it like this:

How We Party - Original Mix

Beware that the code has the word "name" in it several times i.e. name of album, which I don't need. The position of order is always the same so that may help.

I have no idea how to even start going about this, so any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • doutan8506 2014-09-16 17:45
    关注

    That string is JSON. To parse it you would use json_decode(). Then you can access it like an object (or an array if you pass true as the second parameter).

    $str = '{"loading":false,"playing":true,"position":5730,"duration":211875,"index":0,"repeat":false,"shuffle":false,"volume":1,"context":null,"contexts":[],"track":{"artists":[{"name":"R3hab","uri":"spotify:artist:6cEuCEZu7PAE9ZSzLLc2oQ"},{"name":"Vinai","uri":"spotify:artist:4mrBetqy378Jf1y6NLszlx"}],"disc":0,"duration":212000,"image":"spotify:image:9ab80608da23ad97a5fd1cd8285f7a9239169c9d","images":[[64,"spotify:image:544aab2c02ea3ece23145ac3fdb78649e1181cad"],[300,"spotify:image:9ab80608da23ad97a5fd1cd8285f7a9239169c9d"],[600,"spotify:image:8537cde9090567164f449b25c00ad0c40e5c7325"]],"name":"How We Party - Original Mix","number":1,"playable":true,"popularity":67,"starred":false,"explicit":false,"availability":"premium","album":{"uri":"spotify:album:2IhQHv3MS8iq26We33dLdQ"},"local":false,"advertisement":false,"placeholder":false,"uri":"spotify:track:2b0N6oW4f87wNr9uahicc2"}}';
    
    // object
    $obj = json_decode($str);
    echo $obj->track->name;
    
    // array
    $obj = json_decode($str, true);
    echo $obj['track']['name'];
    

    Demo

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

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题