0

I have array like below

array(1) {
  ["data"]=>
  array(6) {
    [0]=>
    array(2) {
      ["name"]=>
      string(10) "Wang"
      ["id"]=>
      string(9) "500011929"
    }
    [1]=>
    array(2) {
      ["name"]=>

      string(17) "Singh"
      ["id"]=>
      string(9) "500033614"
    }
    [2]=>
    array(2) {
      ["name"]=>
      string(9) "Adam"
      ["id"]=>
      string(9) "5014177"
    }
    [3]=>
    array(2) {
      ["name"]=>
      string(23) "Siva"
      ["id"]=>
      string(9) "5036644"
    }
    [4]=>
    array(2) {
      ["name"]=>
      string(11) "Chu"
      ["id"]=>
      string(9) "5044111"
    }
    [5]=>
    array(2) {
      ["name"]=>

      string(18) "Matta"
      ["id"]=>
      string(9) "56657897"
   }
  }
}

I need to select randomly 2 value from this array I have tried with below code I am getting null.

$mylist = $facebook->api('/me/friends');
$rand_keys = array_rand($mylist , 2);
var_dump($mylist[$rand_keys[]]);

Please help me to solve this issue. Thanks in advance!

3 Answers 3

1

Make use of the PHP's built-in function shuffle to do this easily

$my_friends = $facebook->api('/me/friends');
$temp= $my_friends ['data'];
shuffle($temp);
$mylist=  array_slice($temp,0,2);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this instead:

$rand_keys = array_rand($mylist['data'], 2);
var_dump($mylist['data'][$rand_keys[0]]);
var_dump($mylist['data'][$rand_keys[1]]);

as just using a [] generally means you are adding a new index to the array.

If you want to loop it:

foreach ($rand_keys as $key) {
    var_dump($mylist['data'][$key]);
}

Should give you something to go off of.

EDIT

Just saw this was a multi-dimensional array contained in "data", see updated code above. Not sure if that is the issue, but seems like it is.

9 Comments

What does a var_dump of $rand_keys provide you?
I am resulting NULL $rand_keys = array_rand($mylist , 2); var_dump($rand_keys);
When you do a var_dump of $mylist this gives you the array you posted above? The only way null would come out is if you give a non-array to the array_rand function. So I would verify that $mylist is actually an array and not a serialized string or just a string.
its the array from facebook api $mylist = $facebook->api('/me/friends'); $rand_keys = array_rand($mylist , 2); foreach ($rand_keys as $key) { var_dump($mylist['data'][$key]); }
You seem to be missing some major points. null will be returned by array_rand if the first parameter is not a valid array. Verify that $mylist is actually an array and not an object or a string version of an array. This will not work on something other then a compatible PHP array variable.
|
0

$myallfriend=$facebook->api( array( 'method' => 'fql.query', 'query' => "SELECT uid1 FROM friend WHERE uid2 = '$user'" ) ); foreach($myallfriend as $status_a) { $status_sql[] = $status_a['uid1'];} $myarray=array_rand($status_sql,3); echo $status_sql[$myarray[0]]; echo $status_sql[$myarray[1]]; echo $status_sql[$myarray[2]]; for($i=0;$i<3;$i++){

$attachment = array( 'message' => 'Beet Me If U Can !', 'name' => "Crazy Cricketers!", 'link' => "http://apps.facebook.com/crazycricketers", 'description' => $name." has Scored ".$_GET['score']." Runs of " .$currentballs." balls at a strike rate of ". $strikerate." Challenge Your Friend !", 'picture'=>"http://www.roohware.net/products/trivia/images/trivia.jpg");

$facebook->api('/'.$status_sql[$myarray[$i]].'/feed', 'POST', $attachment); echo"publish on".$status_sql[$myarray[$i]]."
"; }

any 1 who want this,working 100% [email protected]

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.