Count array items - php

I want to know how many friends a user has, to select a random friend.
I'm working with the Facebook API and I'm doing this call
$friendlist = $this->facebook->api('/me/friends');
Which returns the array $friendlist
Array (
[data] => Array (
[0] => Array (
[name] => User Name
[id] => XXXXXXXX
)
[1] => Array (
[name] => User name
[id] => XXXXXXXXXX
)
[2] => Array (
[name] => User Name
[id] => XXXXXXXX
)
)
[paging] => Array (
[next] => https://graph.facebook.com/1453123479/friendslimit=5000&offset=5000&__after_id=1000059235606
)
)
I'm trying this - as stated here - but it doesn't echo anything.
echo count($friendlist['data']);

If you would like to select a random friend, you can use the built in PHP function rand() to get a randomly selected index which you can use in your variable $friendlist.
function getRandomFriend() {
$friendlist = $this->facebook->api( '/me/friends' );
// Count the number of friends
$numFriends = count( $friendlist[ "data" ] );
// Random Number
$randNum = rand( 0, $numFriends );
return $friendlist[ "data" ][ $randNum ];
}

You're outputting $data['friendlist'] to get that array so I'd expect to use the following:
echo count( $data['friendlist']['data'] );

Found it!
The number was added to the id - so I couldn't see it - as you can see here:
echo "my id is " . $user_id;
echo count($friendlist['data']);
I'm embarrassed, such a stupid mistake :(.
Thanks for the input tho!
Stijn

Related

array index changing into string [name] not number

I have form (display.php) that will get multiple selected option from user. Then this selected option will be formatted to another page (page.php). The problem occur when I try to display those multiple selected option. The array index are changing into string [name]!
Array ( [0] => 3204120006 [1] => 3204120011 [2] => 3204120010 [3] => 3204120009 )
Array ( [name] => BIRU ) Array ( [name] => BOJONG ) Array ( [name] => MAJAKERTA ) Array ( [name] => MAJALAYA )
Here the code of above display.
<?php
if (isset($_POST["desas"])) {
$ddes=$_POST["desas"];
print_r ($ddes);
foreach ( $ddes as $iddesa ) {
$namadesa=mysql_query("SELECT name FROM villages WHERE id='$iddesa' ");
if ($namadesa) {
$datadesa = mysql_fetch_assoc($namadesa);
print_r($datadesa);
}
} else
$datadesa="";
}
?>
My question is how to change ([name]=>BIRU),([name]=>BOJONG) into index ([0]=>BIRU),([1]=>BOJONG) etc on those array? or something missing in mysql fetch?
you can use array_values. reference: https://secure.php.net/manual/en/function.array-values.php
$datadesa = array_values(array_values);

show random values from array of array

Below is a array generated by a query builder.
$random_array = Array ( [0] => Array ( [text] => A great time was had by all! )
[1] => Array ( [text] => KILL SHOT )
[2] => Array ( [text] => How is it possible)
[3] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
[4] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
)
Currently i am doing like this to print the random value
print_r(array_rand($random_array,1));
This is printing the array key as 3 or 1 etc(random from above array).I want to print the value of the key and not the key.
e.g I want to print the random value like this "http://www.youtube.com/watch?v=KwGOZpbxU9g" or "A great time was had by all!" instead of 3 or 1 which is printing now.
Is it possible to do this.
You will have one more line of code as shown below:
$array_key = array_rand($random_array,1); //get the key
print_r( $random_array[$array_key] ); //use the key to print value
What about simply calling
$randNumber = rand(0,count($random_array))-1; //index in array starts with 0
print (string) $random_array[$randNumber];

Echo certain value from an array

I am trying to echo a certain value of an array which was outputted by an API call.
So when i call
$userid = 42
$api->listServersByOwner($userid)
will output
Array ( [success] => 1 [errors] => Array ( ) [data] => Array ( [Servers] => Array ( [259] => user42 Server ) ) )
What i specifically want is to put the "259" in a variable.
Please do note that this number changes allot depending on the userid so it wont always be 259.
Any help would be great
You mean
$array = $api->listServersByOwner($userid);
$var = key( $array['data']['Servers'] );
Replace $arr with the name of your variable.
echo key( $arr['Servers'] );

Identify which key to modify

I'm using a SESSION variable to hold items added to an ingredients page. I'm wondering how I can uniquely identify each key in the array.
I'm adding ingredients via the following and it's working fine.
$_SESSION['ingredients'][] = array($_POST['ingredient'],$_POST['qty']);
If I stick a few ingredients in there and print the array I get..
Array ( [0] => 1 [1] => 50 ) Array ( [0] => 2 [1] => 50 ) Array ( [0] => 3 [1] => 50 )
Where 1, 2 and 3 are the ingredient IDs.
I can remove ingredients from the array based on their ID no problem, but if I put the same ingredient in twice I won't be able to distinguish between them. I was wondering if I can add an incremental number to ID the key?
Each of the items in $_SESSION['ingredients'] already has a unique index (starting from 0 in your case). When you print your $_SESSION['ingredients'] array, you should get this:
Array ( [0] => Array ( [0] => 1 [1] => 20 ) [1] => Array ( [0] => 2 [1] => 20 ) [2] => Array ( [0] => 1 [1] => 10 ) )
Notice that each array combination has an index preceding it (starting at 0)
The following code demonstrates this:
<?php
session_start();
unset($_SESSION['ingredients']);
$_SESSION['ingredients'][] = array(1, 20);
$_SESSION['ingredients'][] = array(2, 20);
$_SESSION['ingredients'][] = array(1, 10); // adding the same ingredient again
print_r($_SESSION['ingredients']);
?>
Why not use the ingredient id as the key in the session array and then append each value to it as an element
$_SESSION['ingredients'][$_POST['ingredient']][] = $_POST['qty'];
This would give you
Array(
[1] => array(
[0] => 50,
[1] => 50
)
)
Just a thought, I don't know if this would work for your use case
change your inserted array to this:
$_SESSION['ingredients'][count($_SESSION['ingredients'])] = array($_POST['ingredient'],$_POST['qty']);
I use it in my program.

How to append the value in array if key is same

I have an associative array as
[name]->user1,
[class_code]->bsc,
[name]->user2,
[class_code]->msc,
[name]->user1,
[class_code]->mca
Now, i want result as
[name]->user1,
[class_code]->bsc,
mca
[name]->user2,
[class_code]->msc
Means if name is same then append the class_code to first one. How should I do that?
My array is
[1] =stdClass Object
([class_code] =Maths
[userid] =365
[avatar] =default.jpg
[username] =user2
)
[2] =stdClass Object
(
[class_code] =Maths
[userid] = 364
[avatar] =default.jpg
[username] =user1
)
[3] =stdClass Object
(
[class_code] =MCA
[userid] =364
[avatar] =default.jpg
[username] =user1
)
[4] =stdClass Object
(
[class_code] =MCA
[userid] =365
[avatar] =default.jpg
[username] =user2
)
Now, while displaying result i want to display records of for ex. user2 as
username:user2
avatar
classcode :MCA,Maths
and not
username:user2
avatar
classcode :MCA
username:user2
avatar
classcode :Maths
how can I do that?
You can't have two values with the same key. Otherwise php wouldn't know what to give you if you asked for $myarray['name'].
You could use nested arrays instead.
eg
[0]->
[name]->user1,
[class_code]->bsc,mca,
[1]->
[name]->user2,
[class_code]->msc
or you could do something like this
[user1]->bsc, mca
[user2]->msc
if you want to have both values in there, you should consider using an array in there
array(
"name"=>"user1,
"class_code"=>"array('mca','bsc'),
"name"=>"user2,
"class_code"=>"msc"
);
if [class_code] must contain a string, you can try
$array[$key] .= $value;
You can make the whole array multidimensional:
$users = array(
0 = array(
"name"=>"user1",
"class_code"=>"bsc"
),
1 = array(
"name"=>"user2",
"class_code"=>"msc"
)
);
Retrieving a value:
$users[1][name] // Returns 'user2'
$users[0][class_code] // Returns 'bsc'
Cycling through values:
foreach($users as $user => $attributes) {
echo "$user: $attributes[name] ($attributes[class_code])<br />";
}
/*
Prints:
0: user1 (bsc)
1: user2 (msc)
*/

Categories