0

I've been staring at this piece of code for nearly an hour and a half. In my inexperience I've no idea where I'm going wrong.

$gameInfo = array(
    for ($i = 0; $i < 10; $i++) {
        $friendsInfo[$i] = $facebook->api('/' . $randomfriends[$i]);
        array(
            $randomfriends[$i],
            $friendsInfo[$i][first_name],
            $friendsInfo[$i][last_name],
            $friendsInfo[$i][hometown][name],
            $friendsInfo[$i][location][name],
            $friendsInfo[$i][about]
        );
    }
);

This is my attempt. As you can see the values are being pulled in from facebook. This is not the problem as each value appears if I echo. I want to take the values and put them into a multidimensional array that follows this structure.

$gameInfo =  array(
    array('first_name','last_name','hometown','location','about'),
    array('first_name','last_name','hometown','location','about'),
    array('first_name','last_name','hometown','location','about'),
    array('first_name','last_name','hometown','location','about'),
    array('first_name','last_name','hometown','location','about')
);

3 Answers 3

1

Not quite sure what $facebook->api() gives you, but you probably want:

$gameInfo = array();

for($i=0; $i < 10; $i++){
    $gameInfo[] = $facebook->api('/' . $randomfriends[$i]);
}

or

$gameInfo = array();

for($i=0; $i < 10; $i++){
    $friend = $facebook->api('/' . $randomfriends[$i]);
    $gameInfo[] = array(
         $friend['first_name'],
         $friend['last_name'],
         $friend['hometown']['name'],
         $friend['location']['name'],
         $friend['about']
    );
}

Update: If api() returns a JSON string, then you have to use json_decode():

$friend = json_decode($facebook->api('/' . $randomfriends[$i]), true);

No offense, but your whole code is wrong syntax. You might want to read about arrays in PHP.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Felix I'm not sure how this work. I don't think I've explained myself very clearly. $facebook-> returns a json, $randomfriends is an array with the id of each friend I want to return further info about
@willmcneilly: If it returns a JSON string, then you have to use json_decode().
Thank you works perfectly. I totally confused myself with this. No offense taken, I've a lot to learn
1
$gameInfo = array();
for($i = 0; $i < 10; $i++){
    $friendsInfo[$i] = $facebook->api('/' . $randomfriends[$i]);
    $gameInfo[] = array(
      $friendsInfo[$i][first_name],
      $friendsInfo[$i][last_name],
      $friendsInfo[$i][hometown][name],
      $friendsInfo[$i][location][name],
      $friendsInfo[$i][about]
    );
}

Out of curiosity was that code supposed to be like a python list comprehension? or was it from another language?

1 Comment

No sorry it's just me getting the syntax wrong. Thanks for your help.
0
$gameInfo = array();
for($i = 0; $i < 10; $i++){
    $friendsInfo = $facebook->api('/' . $randomfriends[$i]);
    $gameInfo[] = array(
      $friendsInfo['first_name'],
      $friendsInfo['last_name'],
      $friendsInfo['hometown']['name'],
      $friendsInfo['location']['name'],
      $friendsInfo['about']
    );
}

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.