0

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".

1
  • you need to loop the data array Commented Dec 17, 2013 at 23:02

2 Answers 2

1

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"];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this was what i needed! i'll accept your answer in 6 minutes ;)
0

you should do something like this:

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

or run $user_music['data'] in a foreach loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.