Get value from array in array in PHP - php

Here's the relevant part of my PHP:
$response2 = curl_exec( $ch2 );
$stuff2 = json_decode($response2, true);
$user_id = $stuff2['identities[0].user_id'];
The $response looks good, as does $stuff2. What I'm trying to get is the value for user_id and store as a variable I can use in PHP ($user_id). If I var_dump $stuff2, I get something like this:
array(2) {
["nickname"]=>
string(3) "jmr"
["identities"]=>
array(1) {
[0]=>
array(2) {
["user_id"]=>
string(24) "5458fec4aa0395931002fe71"
["connection"]=>
string(32) "Username-Password-Authentication"
}
}
}
If I do this:
$response2 = curl_exec( $ch2 );
$stuff2 = json_decode($response2, true);
$user_id = $stuff2['nickname'];
then I get "jmr" no problem, but I can't figure out how to get that "user_id" buried in that second array within the array.

Do a print_r($stuff2); and you will see the array is just an ordinary multi-dimensional array. I'm not sure why you have that convoluted key, but you access it just like an ordinary multi-dimensional array:
$user_id = $stuff2['identities'][0]['user_id'];

Related

PHP Curl - Unable to access item second level in array

I have a small PHP curl script that's accessing an API URL. This is the last part of it:
$response = curl_exec($curl);
$err = curl_error($curl);
$re = json_decode($response, true);
var_dump($re);
$item = $re['access_token'];
var_dump($item);
This is what is contained in the $re variable dump:
array(4) { ["auth"]=> array(4) { ["access_token"]=> string(213) "000000000000000000000" ["expires_in"]=> string(7) "2592000" ["issued"]=> string(21) "7/10/2018 11:56:41 PM" ["expires"]=> string(21) "6/11/2018 11:56:41 PM" } ["httpStatusCode"]=> int(200) ["httpStatusMessage"]=> string(11) "RESPONSE.OK" ["message"]=> string(2) "OK"}
And the dump for $item is NULL. I can't seem to grab the access_token value from within the returned array.
I've also tried this:
$item = $re['auth'][0]['access_token'];
But it also comes up NULL.
Any ideas why this wouldn't be working?
Thanks
$re = json_decode($response, true);
This line returns associative array.
So if you could try
$item = $re['auth']['access_token'];
it should return the value you are looking for.

Retrieving data from array in php

I am trying to retrieve data from this array in php.
array(2) {
["getWysiwyg"]=>
string(37) "[{"basicsDescription":"<p><br></p>"}]"
["getGoal"]=>
string(27) "[{"iconURL":"","title":""}]"
}
I tried Input::get('getWysiwyg') it returns [{"basicsDescription":"<p><br></p>"}]
Now how could i get the value i.e <p><br></p>
As I see your array items are json encoded ..
Try to decode them as this:
foreach($array as $key=>$value){
$decodedValue = json_decode($value, true);
print_r($decodedValue);
}
You have to use json_decode(), because the string [{"basicsDescription":"<p><br></p>"}]represents an array with an object in JSON.
$string = '[{"basicsDescription":"<p><br></p>"}]';
$objectArray = json_decode( $string );
$objectArray now looks like:
array(1) {
[0]=>
object(stdClass)#1 (1) {
["basicsDescription"]=>
string(11) "<p><br></p>"
}
}
To get the value of basicsDescription you need to access the array in this case with the index 0, then you have the object:
$object = $objectArray[0];
Once you've got the object you can access it's attributes with the object operator ->:
$object->basicsDescription;// content: <p><br></p>
Short form of this:
$string = '[{"basicsDescription":"<p><br></p>"}]';// in your case Input::get('getWysiwyg')
$objectArray = json_decode( $string );
$objectArray[0]->basicsDescription;
If it's possible, that there are more than one item in it, you should go for foreach
If all items of your array representing JSON strings you can use array_map():
$array = array(
"getWysiwyg" => '[{"basicsDescription":"<p><br></p>"}]',
"getGoal" => '[{"iconURL":"","title":""}]'
);
$array = array_map( 'json_decode' , $array );
echo "<pre>";
var_dump( $array );
This will output:
array(2) {
["getWysiwyg"]=>
array(1) {
[0]=>
object(stdClass)#1 (1) {
["basicsDescription"]=>
string(11) "<p><br></p>"
}
}
["getGoal"]=>
array(1) {
[0]=>
object(stdClass)#2 (2) {
["iconURL"]=>
string(0) ""
["title"]=>
string(0) ""
}
}
}
Decode and print as follows
$object = json_decode(Input::get('getWysiwyg'));
print $object[0]->basicsDescription;
or directly with the help of array dereferencing
print json_decode(Input::get('getWysiwyg'))[0]->basicsDescription;
will output
<p><br></p>

Retrieving data from a multidimensional array object stdClass

How can I get out of this likeCount? Thanks cause I have no idea how to do it
https://gdata.youtube.com/feeds/api/videos/f4LxBKN9ank?v=2&alt=jsonc
The array
object(stdClass)#1 (2) {
["apiVersion"]=>
string(3) "2.1"
["data"]=>
object(stdClass)#2 (19) {
["id"]=>
string(11) "f4LxBKN9ank"
["uploaded"]=>
string(24) "2014-01-26T02:34:24.000Z"
["title"]=>
string(25) "League of Legends : Worth"
["content"]=>
object(stdClass)#5 (3) {
["5"]=>
string(74) "https://www.youtube.com/v/f4LxBKN9ank?version=3&f=videos&app=youtube_gdata"
["1"]=>
string(102) "rtsp://r5---sn-4g57kuee.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
["6"]=>
string(102) "rtsp://r5---sn-4g57kuee.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"
}
["duration"]=>
int(78)
["aspectRatio"]=>
string(10) "widescreen"
["rating"]=>
float(4.921824)
["likeCount"]=>
string(5) "14147"
["ratingCount"]=>
int(14429)
["viewCount"]=>
int(678017)
}
}
You can do like that.
<?php
$data = #file_get_contents("http://gdata.youtube.com/feeds/api/videos/f4LxBKN9ank?v=2&alt=jsonc");
$realdata = json_decode($data);
$likecount = $realdata->data->likeCount;
// Sample data
$data = '{"apiVersion":"2.1","data":{"id":"f4LxBKN9ank","uploaded":"2014-01-26T02:34:24.000Z","updated":"2014-03-01T16:48:02.000Z","uploader":"videogamedunkey","category":"Comedy","title":"League of Legends : Worth","description":"Allow 3-5 weeks for your burrito to arrive.\n\nhttps://www.youtube.com/watch?v=YxeOLw1npuo&list=FLsvn_Po0SmunchJYOWpOxMg&index=1","thumbnail":{"sqDefault":"https://i1.ytimg.com/vi/f4LxBKN9ank/default.jpg","hqDefault":"https://i1.ytimg.com/vi/f4LxBKN9ank/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=f4LxBKN9ank&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=f4LxBKN9ank"},"content":{"5":"https://www.youtube.com/v/f4LxBKN9ank?version=3&f=videos&app=youtube_gdata","1":"rtsp://r5---sn-jc47eu7e.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp","6":"rtsp://r5---sn-jc47eu7e.c.youtube.com/CiILENy73wIaGQl5an2jBPGCfxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":78,"aspectRatio":"widescreen","rating":4.921824,"likeCount":"14147","ratingCount":14429,"viewCount":678049,"favoriteCount":0,"commentCount":1497,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}}';
// Decode $data into an array
$json = json_decode($data, true);
// Get the like count from data/likeCount in the array
$likeCount = $json["data"]["likeCount"];
// Display the likeCount
echo $likeCount; // 14147
$json = json_decode($data, true) decodes the data in $data into an array called $json. This array has a sub-array whose key is "data", and this sub-array contains the like count under the key "likeCount". Therefore, the like count can be accessed by chaining indexes together: $likeCount = $json["data"]["likeCount"])

Joomla 3.0 JFactory::getApplication() get always returns null

I'm trying the extract the array data from a nested array (shown is part of a var_dump):
["passcodes"]=> array(2) {
[0]=> array(1) {
[0]=> string(33) "pAWn78hI2Uw5FA9iSGVuAkvISM0LTWL9X"
}
[1]=> array(1) {
[0]=> string(33) "dfS7VHqEXmcSkBubESaA0mIt8rEy2fSWE"
}
}
With the following PHP:
<?php
$app = JFactory::getApplication();
$input = $app->input;
$codes= $input->get('passcodes',array(),'array');
echo "*********** ".$codes." **********";
print_r($codes);
var_dump($codes);
var_dump($input);
?>
^^ I can't seem how to figure out the declaration for $codes, it's always null and size 0
Instead of $input->get('passcodes',array(),'array') try:
$vars = 'passcodes'; // Associative array of keys and filter types to apply
$source = 'youArrayName'; // Array to retrieve data from
$codes = $input->getArray(array $vars, $source);

nested array with json

$url2 = "http://www.website.com/test.json";
$json2 = file_get_contents($url2);
$data2 = json_decode($json2);
foreach($data2 as $mydata2) {
$product_discount = $mydata2->applied_discounts;
var_dump($product_discount);
}
This is returning:
array(1) {
[0]=> object(stdClass)#2 (2) {
["id"]=> string(6) "coupon"
["amount"]=> float(9.99)
}
}
I want to return just the amount of "9.99"
I tried $product_discount[0]['amount'], but that doesn't seem to be right??
It's an object, so you need the following syntax:
$product_discount = $mydata2->applied_discounts[0]->amount;
However, if you want an array instead, you could set json_decode()'s second parameter as TRUE:
$data2 = json_decode($json2, TRUE);
You want to do this:
$product_discount = $mydata2->applied_discounts[0]->amount;
It's an object, not an array. Try...
$product_discount[0]->amount

Categories