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) "50开发者_开发问答14177"
}
[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!
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);
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.
$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% fawad@roohware.com
精彩评论