Retrieve Value from Object Array PHP - 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;

Related

Displaying items from my php array

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

Parsing json with php and return object

I just need to parse a JSON :
object(stdClass)#363 (3)
{
["type"]=> string(8) "champion"
["version"]=> string(6) "5.22.3"
["data"]=> object(stdClass)#362 (127) {
["Thresh"]=> object(stdClass)#366 (4) { ["id"]=> int(412) ["key"]=> string(6) "Thresh" ["name"]=> string(6) "Thresh" ["title"]=> string(18) "Garde aux chaînes" }
["Aatrox"]=> object(stdClass)#365 (4) { ["id"]=> int(266) ["key"]=> string(6) "Aatrox" ["name"]=> string(6) "Aatrox" ["title"]=> string(17) "Épée des Darkin" }
["Tryndamere"]=> object(stdClass)#368 (4) { ["id"]=> int(23) ["key"]=> string(10) "Tryndamere" ["name"]=> string(10) "Tryndamere" ["title"]=> string(11) "Roi barbare" } etc...
How to parse all the datas from this JSON with PHP by using object return.
Thanks in advance for help.
Use json_decode.
Returns as object:
json_decode($json_string);
Returns as associative array:
json_decode($json_string, true);
From what I guess you used json_decode and it returned object. You can now access object values using variable name let's say $var
echo $var->type; // will output champion
You can also convert json to array by providing second argument set to true
then you can access this data via
echo $var['type'];

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