Displaying items from my php array - php

Ive had a lot of help reading some of the questions asked on this site so I figured I would post a question of my own. I've recently been messing around with arrays and was wondering how I would go about displaying certain items from an array I have.
array(1) { [0]=> array(4) { ["kind"]=> string(25) "youtube#videoListResponse" ["etag"]=> string(57) ""blahblah"" ["pageInfo"]=> array(2) { ["totalResults"]=> int(1) ["resultsPerPage"]=> int(1) } ["items"]=> array(1) { [0]=> array(5) { ["kind"]=> string(13) "youtube#video" ["etag"]=> string(57) ""blahblah"" ["id"]=> string(11) "XS6ysDFTbLU" ["snippet"]=> array(9) { ["publishedAt"]=> string(24) "2014-08-15T17:22:04.000Z" ["channelId"]=> string(24) "UCnEiGCE13SUI7ZvojTAVBKw" ["title"]=> string(35) "Bill Gates ALS Ice Bucket Challenge" ["description"]=> string(212) "Bill Gates accepts Mark Zuckerberg’s ALS Ice Bucket Challenge and nominates Elon Musk, Ryan Seacrest and Chris Anderson from TED to participate and raise awareness for ALS, also known as Lou Gehrig’s Disease." ["thumbnails"]=> array(5) { ["default"]=> array(3) { ["url"]=> string(46) "https://i.ytimg.com/vi/XS6ysDFTbLU/default.jpg" ["width"]=> int(120) ["height"]=> int(90) } ["medium"]=> array(3) { ["url"]=> string(48) "https://i.ytimg.com/vi/XS6ysDFTbLU/mqdefault.jpg" ["width"]=> int(320) ["height"]=> int(180) } ["high"]=> array(3) { ["url"]=> string(48) "https://i.ytimg.com/vi/XS6ysDFTbLU/hqdefault.jpg" ["width"]=> int(480) ["height"]=> int(360) } ["standard"]=> array(3) { ["url"]=> string(48) "https://i.ytimg.com/vi/XS6ysDFTbLU/sddefault.jpg" ["width"]=> int(640) ["height"]=> int(480) } ["maxres"]=> array(3) { ["url"]=> string(52) "https://i.ytimg.com/vi/XS6ysDFTbLU/maxresdefault.jpg" ["width"]=> int(1280) ["height"]=> int(720) } } ["channelTitle"]=> string(13) "thegatesnotes" ["categoryId"]=> string(2) "29" ["liveBroadcastContent"]=> string(4) "none" ["localized"]=> array(2) { ["title"]=> string(35) "Bill Gates ALS Ice Bucket Challenge" ["description"]=> string(212) "Bill Gates accepts Mark Zuckerberg’s ALS Ice Bucket Challenge and nominates Elon Musk, Ryan Seacrest and Chris Anderson from TED to participate and raise awareness for ALS, also known as Lou Gehrig’s Disease." } } ["statistics"]=> array(5) { ["viewCount"]=> string(8) "23231956" ["likeCount"]=> string(6) "206532" ["dislikeCount"]=> string(4) "4471" ["favoriteCount"]=> string(1) "0" ["commentCount"]=> string(5) "14548" } } } } }
so this is the array i'm trying to extract values from. Could someone maybe show me how to display the localized title, default thumbnail and viewcount with php. I think from there I can manage to get the rest figured out. Any help would be appreciated. Thanks
P.S.
I've tried to use the below code with no luck.
$MYarray = array($ARRAY_Data);
echo $MYarray['items'][0]['statistics']['viewCount'];
$ARRAY_Data holds the array I posted at the top.

It looks like the thing that's causing you problems is this:
$MYarray = array($ARRAY_Data);
That just adds another array around your original array, which doesn't seem like it should be necessary, and based on your var_dump output, if you hadn't done that, in the next line
echo $MYarray['items'][0]['statistics']['viewCount'];
you're actually already using the correct array keys to get you the value you want. So just skip the $MYarray = array($ARRAY_Data); and use the same keys on the original array:
echo $ARRAY_Data['items'][0]['statistics']['viewCount'];

Related

Retrieve Value from Object Array PHP

I'll go straight to the problem.
I have this array named
$datosUsuario['empleadoUsuario'] = $this->EmpleadoModel->get_empleado_vistaUsuario($result[0]->idusuario);
To show the values from datosUsuario
var_dump($datosUsuario['empleadoUsuario']);
The result speak by himself
array(1) { [0]=> object(stdClass)#26 (13) { ["nombreempresa"]=> string(7) "SERDIRH" ["email"]=> string(23) "luis.gomez#claro.com.ni" ["rutafoto"]=> string(24) "uploads/default-user.png" ["nombrecompleto"]=> string(24) "LUIS FELIPE GOMEZ ROBLES" ["carnetempleado"]=> string(6) "400387" ["direccion"]=> string(13) "SIN DIRECCION" ["telefono"]=> string(8) "88330377" ["dominioempleado"]=> string(10) "luis.gomez" ["fecha_ingreso"]=> string(10) "11/06/2012" ["nombreregion"]=> string(19) "CENTRO Y ESPECIALES" ["nombredepartamento"]=> string(4) "RAAS" ["nombretienda"]=> string(10) "BLUEFIELDS" ["idtienda"]=> int(81) } }
and when i try to obtain the value idtienda just gave me a NULL.
$data['idtienda'] = $empleadoUsuario[0]->idtienda;
Please tell me what im doing wrong.
The framework it's codeigniter.
Replace this
$data['idtienda'] = $empleadoUsuario[0]->idtienda;
with
$data['idtienda'] = $datosUsuario['empleadoUsuario'][0]->idtienda;

Having trouble getting a specific value in JSON/PHP

I am trying to pull one single piece of data from a json encoded string. I managed to get the string into php's jason format, but I dont understand how to display the exact data I want (confirmations) Every time I try to access any of the data it just gives me a bracket({) and no data. I know I'm doing something wrong with retreiving the data from the variable, but I can't pu tmy finger on it.
Here's the data I want to parse, could someone please show me how to access the confirmations value from within the array of data? Thanks.
( https://projectbuilder.info/link.php?ID=jsontest&CMD=page ) [easier to read]
Source Code:
<?PHP
$return = file_get_contents("https://api.chain.com/v2/bitcoin/transactions/76e6f17cb940745255e2b8439eea5dae945a b148f1fbba98a9fb99c9a5801320?api-key-id=ae7317a1cd4ff0d12e49a77bfd8b9dec");
var_dump(json_decode($return));
echo $return[0]["confirmations"]; //one of my many attempts to get the info, also tried without the leading zero, and different numbers. I understand arrays, I just dont understand how the json data is formatted inside the array.
?>
Output Copy(what do I do with $return to get it's confirmation value of 2896?):
object(stdClass)#1 (11) {
["hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["block_hash"]=>
string(64) "00000000000000001646d024d4622a0e4a5c06299d7d776de041bc9c317be1f8"
["block_height"]=>
int(329486)
["block_time"]=>
string(20) "2014-11-11T02:10:21Z"
["chain_received_at"]=>
string(24) "2014-11-11T02:05:48.259Z"
["confirmations"]=>
int(2896)
["lock_time"]=>
int(0)
["inputs"]=>
array(2) {
[0]=>
object(stdClass)#2 (7) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_hash"]=>
string(64) "4f7b6066396e422f1cabd60767093ec6fb4480b60f206c408ad50895541bc023"
["output_index"]=>
int(0)
["value"]=>
int(144475)
["addresses"]=>
array(1) {
[0]=>
string(34) "1HPvAS96JXYUuLDs5CKNh61SvH6NJT1ykH"
}
["script_signature"]=>
string(213) "304602210087289ed01fd7d04e3c7eb5c38ea1944cbc3789658a1122610079a4f0421e2426022100fca6f5f4623bbac131f06ebdc7389ea0c76b4355da9508ecc7b02107385ee79d01 029a79a3cf6f8b90b7c1210e593a21e46c81ffbbe544eb2ab3ebbd89f33e4f8b2e"
["sequence"]=>
int(4294967295)
}
[1]=>
object(stdClass)#3 (7) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_hash"]=>
string(64) "22de0dbb8d72fac9a6e8775f6f80fa3fc991c41d33fcd5081acb49c0479f2a62"
["output_index"]=>
int(1)
["value"]=>
int(7390022)
["addresses"]=>
array(1) {
[0]=>
string(34) "158kR5o6EWWhFEZfLqGDvkgfoyi2Ep2fhA"
}
["script_signature"]=>
string(209) "304402205e2f36c9c22e02767e3accc8bc609b74d5a23c58e4a8edbac24cb4baf1e3feaa022043ad33becd57c5dee03fd1c1d8be1524281f6f49ba1e44a1a2b340f89554c42d01 031181694e14973f71d45f5f3ab73ee0f30dfa3f488bf53c44e46cf9f4f3d3d722"
["sequence"]=>
int(4294967295)
}
}
["outputs"]=>
array(2) {
[0]=>
object(stdClass)#4 (9) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_index"]=>
int(0)
["value"]=>
int(144475)
["addresses"]=>
array(1) {
[0]=>
string(34) "1P5rwnk3GYbxgpxN9M9EziLrpvoih4c8JC"
}
["script"]=>
string(85) "OP_DUP OP_HASH160 f23e1f6dd21bab989f18c14f26bf37b4e2372eef OP_EQUALVERIFY OP_CHECKSIG"
["script_hex"]=>
string(50) "76a914f23e1f6dd21bab989f18c14f26bf37b4e2372eef88ac"
["script_type"]=>
string(10) "pubkeyhash"
["required_signatures"]=>
int(1)
["spent"]=>
bool(false)
}
[1]=>
object(stdClass)#5 (9) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_index"]=>
int(1)
["value"]=>
int(7380022)
["addresses"]=>
array(1) {
[0]=>
string(34) "1DpY5Mu2qTNkwsnPwgqFnaZ1Kq7GpKDboy"
}
["script"]=>
string(85) "OP_DUP OP_HASH160 8c9efff6e8500a36c16a939054a333d81ef23166 OP_EQUALVERIFY OP_CHECKSIG"
["script_hex"]=>
string(50) "76a9148c9efff6e8500a36c16a939054a333d81ef2316688ac"
["script_type"]=>
string(10) "pubkeyhash"
["required_signatures"]=>
int(1)
["spent"]=>
bool(true)
}
}
["fees"]=>
int(10000)
["amount"]=>
int(7524497)
}
{
Have a look at the docs: http://php.net/json_decode
Convert the json-string to a php-array and save it in a variable. Then use it like any other array to get values from it. Like this:
$return = file_get_contents('http://etc.com/');
$decoded_return = json_decode($return);
echo $decoded_return['confirmations']; //Make sure the key exists, obviously
So keep in mind that $return is a string. The json_decode function returns an array, so you have to save that array in a (new - preferably) variable to get values from it.

Another simple PHP JSON DECODE challenge

I have some JSON that I am trying to decode -- it is correctly extracting the value, but also generating an error.
Here is the var_dump:
["success"]=>
bool(true)
["providers"]=>
array(2) {
[0]=>
object(stdClass)#5 (15) {
["address1"]=>
string(14) "3240 W Lake St"
["address2"]=>
NULL
["city"]=>
string(11) "Minneapolis"
["crossStreet"]=>
string(26) "Lake Street & Market Plaza"
["description"]=>
string(55) "test location28402 description of services/prices/hours"
["distance"]=>
float(0.42900015862223)
["lat"]=>
float(44.948469)
["lon"]=>
float(-93.321155)
["name"]=>
string(17) "testlocation28402"
["phone"]=>
string(10) "6125551212"
["precise"]=>
bool(true)
["state"]=>
string(2) "MN"
["url"]=>
string(41) "http://www.testlocation28402.com?id=28402"
["urlCaption"]=>
string(25) "www.testlocation28402.com"
["zip"]=>
string(9) "554164512"
}
[1]=>
object(stdClass)#6 (15) {
["address1"]=>
string(19) "4335 Excelsior Blvd"
["address2"]=>
NULL
["city"]=>
string(16) "Saint Louis Park"
["crossStreet"]=>
NULL
["description"]=>
string(55) "test location26358 description of services/prices/hours"
["distance"]=>
float(0.91979730006713)
["lat"]=>
float(44.935773)
["lon"]=>
float(-93.33489)
["name"]=>
string(17) "testlocation26358"
["phone"]=>
string(10) "6125551212"
["precise"]=>
bool(true)
["state"]=>
string(2) "MN"
["url"]=>
string(41) "http://www.testlocation26358.com?id=26358"
["urlCaption"]=>
string(25) "www.testlocation26358.com"
["zip"]=>
string(9) "554164811"
}
}
["errors"]=>array(0) {
}
}
Above is the var_dump of the variable $json. I am trying to extract the CITY field with:
$json = json_decode($surescript);
foreach ($json as $providers){
foreach($providers as $onespot) {
echo "*";
echo $onespot->city;
echo "\n";
}
}
This outputs:
Warning: Invalid argument supplied for foreach() in /home/content/96/7973196/html/channels/MIL432/storeFinder_2.php on line 153
*Minneapolis
*Saint Louis Park
So, it is getting the correct nested variable, but also throwing off an error on the second foreach loop.
What am I doing incorrectly?
Thanks in advance for your help.
You're iterating over everything in the JSON in your outer loop, meaning that it's trying to parse the success section, the providers section, as well as the errors section.
When you try to parse the success, you can't iterate over the contents in the inner loop, hence the error. The errors section is an array, so you can iterate over it - since there's nothing in it, no error/warning for trying to access city, which wouldn't exist in it.
To only parse the providers:
$json = json_decode($surescript);
foreach ($json->providers as $provider){
echo "*";
echo $provider->city;
echo "\n";
}

How do I get the data out of this array? Single sign on

I have implemented a Single Sign on function using Janrain. It outputs this data (amongst other data). Is it possible to break this down and extract only the displayName for example so that I may place it in a variable?
auth_info:
array(3) {
["stat"]=>
string(2) "ok"
["profile"]=>
array(10) {
["providerName"]=>
string(8) "Facebook"
["identifier"]=>
string(48) "removed"
["preferredUsername"]=>
string(10) "OllieJones"
["displayName"]=>
string(11) "Ollie Jones"
["name"]=>
array(3) {
["formatted"]=>
string(11) "Ollie Jones"
["givenName"]=>
string(5) "Ollie"
["familyName"]=>
string(5) "Jones"
}
Here is the script that creates it and where I would like to define the variable https://github.com/janrain/Janrain-Sample-Code/blob/master/php/rpx-token-url.php
Many Thanks
Try $auth_info['profile']['displayName']

How to sort php array by value and add an array to another

Okay, so I'm writing an app that allows me to see steam data from a database of whoever registered.
I met a problem. Firstly, the steam API for multiple users is not standardized. (e.g. everytime you refresh this, the position of user changes (What kind of API does this?!)
Since steam does not standardize the API, I'll have to do it myself, so after doing a json_decode($url, true). It is not an assoc array.
I want to sort the assoc array by the steam ID (which is numeral) and match them against my own database of user (also contains steam ID, but can be sorted in the database), so how do I go about doing that?
E.g.
Array 1:
array(3) {
[0]=>
array(2) {
["steam_id32"]=>
string(17) "76561198025035234"
["name"]=>
string(7) "Mitsuki"
}
[1]=>
array(2) {
["steam_id32"]=>
string(17) "76561197968270056"
["name"]=>
string(3) "nrn"
}
[2]=>
array(2) {
["steam_id32"]=>
string(17) "76561197982490298"
["name"]=>
string(4) "Ximp"
}
}
Array 2:
array(1) {
["response"]=>
array(1) {
["players"]=>
array(3) {
[0]=>
array(16) {
["steamid"]=>
string(17) "76561197982490298"
["communityvisibilitystate"]=>
int(3)
["profilestate"]=>
int(1)
["personaname"]=>
string(53) "……‮‮‮‮‮‮‮‮‮‮Ximp ……FUS RO DAH"
["lastlogoff"]=>
int(1328569605)
["profileurl"]=>
string(34) "http://steamcommunity.com/id/ximp/"
["avatar"]=>
string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1.jpg"
["avatarmedium"]=>
string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1_medium.jpg"
["avatarfull"]=>
string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/f8/f8ee0cf00a2ec20417bf5b26b99fd6fb4dc176c1_full.jpg"
["personastate"]=>
int(1)
["realname"]=>
string(9) "I life in"
["primaryclanid"]=>
string(18) "103582791430354400"
["timecreated"]=>
int(1146939839)
["gameextrainfo"]=>
string(20) "The Binding Of Isaac"
["gameid"]=>
string(6) "113200"
["loccountrycode"]=>
string(2) "DE"
}
[1]=>
array(14) {
["steamid"]=>
string(17) "76561197968270056"
["communityvisibilitystate"]=>
int(3)
["profilestate"]=>
int(1)
["personaname"]=>
string(3) "nrn"
["lastlogoff"]=>
int(1328618220)
["profileurl"]=>
string(34) "http://steamcommunity.com/id/nrnx/"
["avatar"]=>
string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1.jpg"
["avatarmedium"]=>
string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1_medium.jpg"
["avatarfull"]=>
string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/50/50b908e0aa2c730fa0f68ab0afc8b04fddb133f1_full.jpg"
["personastate"]=>
int(1)
["realname"]=>
string(9) "Nathaniel"
["primaryclanid"]=>
string(18) "103582791432850562"
["timecreated"]=>
int(1092771678)
["loccountrycode"]=>
string(2) "US"
}
[2]=>
array(14) {
["steamid"]=>
string(17) "76561198025035234"
["communityvisibilitystate"]=>
int(3)
["profilestate"]=>
int(1)
["personaname"]=>
string(23) "[ProudiA] Mitsuki Sakai"
["lastlogoff"]=>
int(1328621807)
["commentpermission"]=>
int(1)
["profileurl"]=>
string(42) "http://steamcommunity.com/id/mitsukisakai/"
["avatar"]=>
string(114) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719.jpg"
["avatarmedium"]=>
string(121) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719_medium.jpg"
["avatarfull"]=>
string(119) "http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d279f349422cbbed55adf1c8eabb0924ea0a719_full.jpg"
["personastate"]=>
int(1)
["realname"]=>
string(12) "酒井å‚è¼"
["primaryclanid"]=>
string(18) "103582791432752089"
["timecreated"]=>
int(1273714689)
}
}
}
}
For sortig array you can find a list of all function that you need here
Update:
first you must create a 1d array from a 2d or 3d you can use this code to make an easy access array and sortable (this an example):
<?php
$inArr;//This is the 2D array
$outArr = array();
for($i=0;$i<count($inArr);$i++){
$outArr[$i] = $inArr[$i][0];
?>
then you can sort it with ksort() or krsort() function.and for adding an array to another :
<?php
$stack = array("value1", "value2");
array_push($stack, "value3", "value4");
print_r($stack);
?>

Categories