Retrieve value from array in PHP [duplicate] - php

This question already has answers here:
Get values stdClass Object PHP
(1 answer)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
If I have a array formatted as below
[
{
'id':1,
'some_int': 3,
'foo':'bar'
}
]
How can I retrieve, for example, 'bar' value?
If I write
$array[0]['bar']
It throws Cannot use object of type stdClass as array
If I write
$array['bar']
It throws Unidentified index: bar

Related

How to extract decode JSON from REDCap Array with PHP [duplicate]

This question already has answers here:
Why does this PHP code just echo "Array"?
(6 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have an JSON Export from a REDCap and it started as a nested ARRAY. I use json_decode, but when I print the result I get an ERROR.
Here is the Array:
[{"record_id":"13","lastname":"Mustermann","first_name":"Json","dob":"1948-10-12","case_number":"1366613"}]
My Code is:
$deco = json_decode($json);
print $deco;
The line "echo" gives an error. What am I doing wrong?!
Thank you

JSON array convert to PHP Array [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have a json object array. I want to read the this value. how to read value using php.
My json object Array
[{"employeeId":"ioc-nugegoda","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"},{"employeeId":"ioc-nugeqqq","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"},{"employeeId":"ioc-nusqq","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"}]
$jsonObject = json_decode('{"employeeId":"ioc-nugegoda","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"},{"employeeId":"ioc-nugeqqq","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"},{"employeeId":"ioc-nusqq","employeeEmail":"lankaioc#lankaioc.com","employeeName":"Colombo","employeePhoneNum":"12345","employeeBrithday":1334946600000,"employeeHomeTown":"ykjld"}');
Which you can find here. After that you can access all values of the json object by its key:
echo $jsonObject["employeeId"];
//Should return: ioc-nugegoda
If you have an array arround your JsonObject you need to access the object itself first.. this would look like:
echo $jsonObject[0]["employeeId"];

Why does an object behave differently after a print_r / var_dump [duplicate]

This question already has answers here:
print_r() adds properties to DateTime objects [duplicate]
(7 answers)
Why can't I access DateTime->date in PHP's DateTime class?
(5 answers)
Closed 5 years ago.
Can anyone explain why these two pieces of code act differently and what I'm doing wrong:
print_r($user['first_signed_up_date']);
echo $user['first_signed_up_date']->milliseconds/1000;
Result:
MongoDB\BSON\UTCDateTime Object
(
[milliseconds] => 1507976375000
)
1507976375
Second code:
// removed print_r / var_dump
echo $user['first_signed_up_date']->milliseconds/1000;
Result:
0
The only difference between first and second code is there's no print_r (var_dump has same result)

PHP Get Array Value from JSON Not working [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I want to get the URLS from the JSON below.
$jsonArray = {
"uuid": "signed",
"PreSigned": "{'url': ['www.g.com', 'www.o.com']"}
I tried this $jsonArray -> PreSigned[0]->url
And it didn't work

reference object with key containing brackets [duplicate]

This question already has answers here:
PHP Object Property has brackets in it
(2 answers)
Closed 9 years ago.
I'm doing a print_r on an object that is returned after running a query. Here is my print_r log.
stdClass Object
(
[MAX(sort_order)] => 3
)
I want to get the value inside [MAX(sort_order)] but I cant figure out how to target it from within php.
Like $sort_order = $object->[MAX(sort_order)]; (I know that won't work)
Does anyone know how I can do this?
Try add this in your query MAX(sort_order) AS max_sort_order in your query

Categories