Echo certain value from an array - php

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'] );

Related

php count not working properly in my array

i print php array when it's empty it print 1
$addresses = $user->myFunction();
print_r(count($addresses['my_test']));
Array
(
[my_test] => Array
(
[test] => test
[test1] => test1
)
)
it print 2
when i print this
Array
(
[my_test] => Array
(
[] =>
)
)
i got 1
i don't know what is the problem here
How to print exact value of this?
Array count all element including empty ones. Your output is correct as you second array has 1 element.
Consider use array_filter to avoid them.
Example:
$a = array("" => "");
echo count($a). PHP_EOL; // echo 1
$a = array_filter($a);
echo count($a). PHP_EOL; // echo 0
In you case:
print_r(count(array_filter($addresses['my_test'])));
Array
(
[my_test] => Array
(
[test] => test
[test1] => test1
)
)
print_r(count($addresses['my_test'])); // it will show 2 cause you have 2 values inside my_test key.
print_r(count($addresses)); // it will show 1 cause you have one value inside main array
Array
(
[my_test] => Array
(
[] =>
)
)
print_r(count($addresses['my_test'])); // it will show 0 because you have 0 values inside my_test key.
print_r(count($addresses)); //it will show 1 because you have one value my_test inside your main array.
Hope it helps you clarify count function.

retrieve specific key value from multidimensional array

I have the following array, since i converted the string i got back from a SOAP call to an array:
Array
(
[soapenvBody] => Array
(
[queryRequestsResponse] => Array
(
[result] => Array
(
[0] => Array
(
[BCRcustomId] => REQ16569
[BCRexternalId] => Array
(
)
[BCRrecordId] => a035700001CM60kAAD
[BCRrequestId] => a1J5700000857EYEAY
)
[1] => Array
(
[BCRcustomId] => SRQ100784
[BCRexternalId] => Array
(
)
[BCRrecordId] => a033E000001PxfAQAS
[BCRrequestId] => a1J3E0000000GSaUAM
)
)
)
)
)
I am trying to retrieve the BCRrecordId, since I need that item to make another SOAP call. I tried the following
function getID($array) {
return $array['BCRcustomId'];
}
//
$arr = array_map('getID', $array );
print_r($arr);
Now i get an error back on this saying it doesnt find it.
Undefined index: BCRcustomId in
index.php on line 97
[soapenvBody] => )Array (
My assumption is that it doenst go lower than 1 level in the array. Now i am not familair with these kinds of arrays, how would I solve this? By multiple for each loops? Or is there another way to retrieve these items
If $array is the whole response, you need to pass only result part of it:
$arr = array_map('getID', $array['soapenvBody']['queryRequestsResponse']['result']);

How can I get the meta out of this array in a variable?

I have another (may stupid) question:
How can I get the "Olaf" (or everything else what is there) from the meta in this array in a $variable ?
Array (
[0] => Pagekit\Blog\Model\Post Object (
[id] => 1
DateTimeObject (
[bla] => Bla
)
Pagekit\User\Model\User Object (
[bla] => bla
)
[meta] => Array (
[og:description] => Olaf
)
)
)
Thanks for your help.
I see meta is a key under object of [0]th key of main array. You should be able to get the values using ->.
$var1 = $test[0]->meta["og:description"];
Try this:
$meta_desc = $arr[0]->meta['og:description']
I understand that first element of array is an object.

Put a key of end of an array in php

I have this array:
Array
(
[0] => Array
(
[date] => 2016-03-08
[value] => Array
(
[key_1] => Array
(
[test_1] => 1
[test_2] => 10
[test_3] => 1000
[test_4] => 200
)
[key_2] => Array
(
[test_1] => 1
[test_2] => 15
[test_3] => 1500
[test_4] => 100
)
)
)
Now I have another array :
Array
(
[key_3] => Array
(
[test_1] =>
[test_2] =>
[test_3] =>
[test_4] => 1
)
)
I want to add this last array in the first array.
I try like this : array_push($ymlParsedData[]['value'], $a_big_gift); but not work. Can you help me please ?
You can't use $ymlParsedData[] for accessing specific element, it is a shorthand for pushing data to array.
You can use either
// NB! array_push() just adds the values, key 'key_3' is removed
array_push($ymlParsedData[0]['value'], $a_big_gift);
or
// will keep key 'key_3'
$ymlParsedData[0]['value']['key_3'] = $a_big_gift['key_3'];
or
// use array_merge() instead
$ymlParsedData[0]['value'] = array_merge($ymlParsedData[0]['value'], $a_big_gift);
A complicated answer, but this might solve your issue:
$key_name = array_keys($a_big_gift)[0];
$ymlParsedData[0]['value'][$key_name] = $a_big_gift[$key_name];
echo '<pre>'; print_r($ymlParsedData); exit;
Note: For making it dynamic and for more than one value of $a_big_gift, you need to loop it and achieve your result.
Try this
array_push($ymlParsedData[0]['value'], $a_big_gift['key_3']);

Count array items

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

Categories