PHP - Object converted to Array, now need a Value - php

I've managed to confuse myself here, I have an object that I've converted to an array and now want to get the value of:
echo var_dump((array)$obj['country_id']);
Gives an output of:
array(1) { [0]=> string(2) "GB" }
How do I simply get "GB"?

try this:
$result = (array)$arr['country_id'];
echo $result[0];

Related

Name of array in result of json_decode() [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
If I do a json_decode like this:
$json = json_decode($jsondata);
echo var_dump($json);
I get something like this:
object(stdClass)#1 (1)
{ ["QRY_JISGesch"]=> array(3)
{ [0]=> object(stdClass)#2 (8)
{ ["JISGeschID"]=> int(7) ["StandorteID"]=> int(0) ["FSKID"]=> int(23)
}
[1]=> object(stdClass)#3 (8)
{ ["JISGeschID"]=> int(8) ["StandorteID"]=> int(0) ["FSKID"]=> int(22)
}
[2]=> object(stdClass)#4 (8)
{ ["JISGeschID"]=> int(9) ["StandorteID"]=> int(0) ["FSKID"]=> int(1)
}
}
}
How do I find out "QRY_JISGesch" in code?
You could use reset() to get the first member of an object (or an array).
$json = json_decode($jsondata);
$first = reset($json);
If you only want the get the first "key", you can use key();
$json = json_decode($jsondata);
$key = key($json); // QRY_JISGesch
Try this. It will help you :-
$json = json_decode($jsondata, true);
// true means objects will be converted into associative arrays
and you can access QRY_JISGesch like this way :- $json['QRY_JISGesch']
Is there only one item given in the array, and you are not interested in the name itself, but the values? Then array_values could help.
Otherwise, you could use array_keys to read all keys and iterate over them? Or use a foreach loop which does not care about the array key neither.
get_object_vars (object $object) will give you a list of all accessible properties of the returned object.

how to access key pair value of query result in php

I am trying to get the value of currentPdfDownload and max_download_limit from my $result array variable
var_dump($result); output:-
array(1) {
[0]=>
object(stdClass)#1529 (2) {
["currentPdfDownload"]=>
string(2) "42"
["max_download_limit"]=>
string(2) "20"
}
}
I try this:
echo $result['currentPdfDownload']." ".$result['max_download_limit'];
But it does not give any result.
Please highlight what I am doing wrong?
Thanks in advance
You need to do it like below:-
echo $result[0]->currentPdfDownload." ".$result[0]->max_download_limit;
Note:- Your array have an index 0 and on that index it have an object array so you need to use [0] and -> to fetch it's indexes as property of an object

How to get a variable value in a stdClass object?

I get a returned result from an API in stdClass format. I don't know anything about this type of data. So here it looks like:
object(stdClass)#41 (1) {
["return"]=>
object(stdClass)#42 (7) {
["afterPayOrderReference"]=> string(32) "d4ab78df6ab2ef84194dd1c1d66240b8"
["checksum"]=> string(32) "4f8826a99e9c0a67e578d04b6a625117"
["resultId"]=> int(0)
["statusCode"]=> string(1) "A"
["timestampIn"]=> float(1408533108515)
["timestampOut"]=> float(1408533113616)
["transactionId"]=> int(129525)
}
}
What I need is retrieving the statusCode value. I tried doing like in a post I read:
$array = (array) $stringResult;
$array[0]->statusCode;
But it didn't work. Please, someone explain to me in the simplest way because it's really new to me. Thanks.
Object properties are accessed with the -> operator. Just do:
echo $stringResult->return->statusCode;
If you wanted an array you would access like this since the array contains an object:
$array = (array)$stringResult;
echo $array['return']->statusCode;
Its an object array, so just as you call the array element,
echo $stringResult["return"]->statusCode

How can two arrays created in the same way become different?

The var_dumps of the two arrays are different but they were created the exact same way . Why is this?????
first array dump:
array(2) {
[0]=>
array(0) {
}
[1]=>
string(6) ",2,2,1"
}
second array dump:
array(3) {
[0]=>
array(0) {
}
[1]=>
string(1) "2"
[2]=>
string(1) "2"
[3]=>
string(1) "1"
}
array 1 is made on fileA.php
while ($row = mysql_fetch_assoc($res))
{
$iState[] = $row["state"];
}///end while
then i use ajax to send to fileB.php
js_array=<? echo json_encode($iState); ?>;
var url_js_array=js_array.join(',');
xmlhttp.open("GET","fileB.php?istate="+ url_js_array,true);
xmlhttp.send();
then in fileB.php(ajax response file) i retrieve the array
$iStateValues[] =$_GET["istate"] ;
then I create array 2 on fileB.php
while ($row = mysql_fetch_array($res))
{
$currentiState[]= $row["state"];
}///end while
then I compred the two
echo"\n\nsame test\n";
if($iStateValues==$currentiState)
echo "same";
else
echo "not same";
The problem:
Currently, you're sending the comma-separated string to fileB.php.
$iStateValues[] = $_GET["istate"] ;
$_GET["istate"] contains the comma-separated string, and in the above statement, you're pushing the value into an empty array $iStateValues.
Now, in fileB.php, you create another array $currentiState using a while loop. Then you try to compare them.
$iStateValues is a string, not an array. $currentiState is a real array. So the comparison will always return FALSE.
How to fix the issue:
Instead of sending the comma-separated string to fileB.php, send the actual JSON string. So your code will look like:
js_str=<? echo json_encode($iState); ?>;
xmlhttp.open("GET","fileB.php?istate="+ js_str,true);
xmlhttp.send();
Now, in fileB.php, you can receive the JSON string and then decode it, like so:
$jsonArray = json_decode($_GET['istate'], true);
(The second parameter in JSON decode says it should be decoded to an associative array instead of an object).
Then do the comparison.

Get elements of json_encoded array in javascript

i passed a json_encoded array to javascript. Now i would like to acces that array to get the different elemtnts.
i print it out in the console.log() and i get this array:
array(1) {
[16]=>
array(2) {
[3488]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "999184"
["article_name_internal"]=>
string(29) "Geschenkbox Kerzenschein 2011"
}
}
[2615]=>
array(1) {
[0]=>
array(2) {
["article_no_internal"]=>
string(6) "700469"
["article_name_internal"]=>
string(29) "Hotelscheck RomantischeTagef2"
}
}
}
}
This is about right. How can i access the article_name of the second array, with the ID 2615?
found a related question here reading a jsone object, hope for some better explebation or answer. Thanks.
EDIT:
As it seems i made a mistake, i showed a php var_dump in the console. When i try to show the javascript array in the console i get undefined.
Since JSON means "JavaScript Object Notation" you don't need to do anything to access the object's items.
For example you can access:
jsonObject[2615][0]["article_name_internal"]
if this object is String, use eval to convert the string to a JavaScript object and access the items in the same way with the previous example.
var jsonObject = eval(jsonstring);
jsonObject[2615][0]["article_name_internal"]

Categories