tags:

views:

41

answers:

2

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!

A: 

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.

Brad F Jacobs
Not working same issue NULL
Elankeeran
What does a `var_dump` of `$rand_keys` provide you?
Brad F Jacobs
I am resulting NULL $rand_keys = array_rand($mylist , 2);var_dump($rand_keys);
Elankeeran
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`.
Brad F Jacobs
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]);}
Elankeeran
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.
Brad F Jacobs
How to verify Premiso. Could you please guide me I am beginner in PHP
Elankeeran
http://www.php.net/is_array so a: `if (is_array($mylist)) { echo "its an array!"} else { echo "Nope, it is not an array"; }` would be a simple test, but a `var_dump` of `$mylist` should also tell you (the very top portion) of what type it is.
Brad F Jacobs
The code resulting its an array.
Elankeeran
Premiso any update... please help me
Elankeeran
I have no clue then. But the code I posted, given a valid array produces the expected data. So somewhere along the line there is an issue, without seeing the extra code surrounding the function, it is hard to tell (if you choose to show extra code modify the question).
Brad F Jacobs
No I am not using any extra code... whatever code available above same i am using.
Elankeeran
+1  A: 

Hi Keeran,

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);
Markish