donglin9068 2013-12-17 22:59
浏览 42
已采纳

从数组中获取字段

I'm working on a facebook app to retrieve information from the user. The overall goal is to make a "interactive story" about the user. So I want the name, birthday etc. But I also want to know their favorite bands. The problem is that I can display the array with all the info needed. But that is to much. I don't want to know the category or the id, I only want to know the name.

$user_music = $facebook->api('/me/music');
print_r ($user_music);

If I use this code, I get something like this:

Array ( [data] => Array ( 
[0] => Array ( [category] => Musician/band [name] => Red Hot Chili Peppers [created_time] => 2011-03-21T15:01:57+0000 [id] => 8335563918 ) 
[1] => Array ( [category] => Musician/band [name] => Train [created_time] => 2011-03-21T15:01:57+0000 [id] => 15313895735 ) ) 

I only want the bands name, but if I use this code:

$user_music = $facebook->api('/me/music');
$music_name = $user_music["name"];
echo $music_name;

it says it doesn't know "name".

  • 写回答

2条回答 默认 最新

  • drmgg4411 2013-12-17 23:02
    关注

    You missed 1 array level. To access "name" you should use:

    $user_music["data"][0]["name"]
    

    or to get all names you should iterate

    foreach($user_music["data"] as $data) {
        echo $data["name"];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧