How to get the values from the following array? - php

I got the following array as a result of an output from a web service? I printed the values in the array using the print_r() method as stated in the following description.
ARRAY OUTPUT:
Array
( [0] => stdClass Object
( [return] => stdClass Object
( [data] => stdClass Object
(
[status] => 50000
[adminUser] => 1
[atdUserid] => 58
[category] => [client] => [cur_designation] => TL
[currentEmpId] => E058
[digitPrefix] => 8,5,1,3,7,0
[email] => jaliya#codegen.net
[employeeId] => 58
[employee_status] => 1
[firstName] => Jaliya
[lastName] => Seneviratna
[last_login_date] => stdClass Object ( [date] => 6 [month] => 2 [year] => 2015 )
[letterPrefix] => D,C,U,T,Z,E
[loginName] => jaliya
[resourceStatus] =>
[taskPassword] => d6188c72995d80e1a8e00d34987e0f6b
[userId] => 118 )
[reason] => Success
[refetch] => 1
[status] => 1
) ) )
I got the above array by calling a webservice in php. I want to get the details out of this array. And the problem was that I couldn't get the stdClass objects casted into the right type. I tried the following code but it is not working. Can anyone help me to get the values inside the data[] out in php. I used the following code and it is not working and giving an exception.
CODE USED:
print_r(array_values($quote));
echo $quote[0]->data;
The exception was the following...
EXCEPTION RECIEVED:
Notice: Undefined property: stdClass::$data in C:\xampp\htdocs\WebServiceDemo-php\democlient.php on line 27
How to get values out from this array?
Please help me...

convert the object in to array using this function
get_object_vars(array);

USE Can Print each key value of data array like this:-
echo $quote[0]->data->status;
I will Print
50000

Related

Cannot access protected property PHP

trying to get values from object array, here is error and what i tried so far.
echo "<pre>";print_r($response->_value());//Call to undefined method OmiseCharge::_value()
echo "<pre>";print_r($response->_value); //Cannot access protected property PHP
actual array :
OmiseCharge Object
(
[OMISE_CONNECTTIMEOUT:OmiseApiResource:private] => 30
[OMISE_TIMEOUT:OmiseApiResource:private] => 60
[_values:protected] => Array
(
[object] => list
[from] => 2012-08-01T00:00:00+00:00
[to] => 2016-10-20T00:00:00+00:00
[offset] => 0
[limit] => 20
[total] => 201
[order] => chronological
[location] => /charges
[data] => Array
(
[0] => Array
(
[object] => charge
[id] => chrg_test_##############
[livemode] =>
echo "<pre>";print_r($response->offsetGet('data'));
worked thanks.
to access protected member of a call you need to implement getter method in class which are type of public.

json_encode does not work with strings as indexes

When I have an array like this :
Array (
[0] => Array ( [0] => 1 [1] => 12 [2] => Essener [3] => 1 )
[1] => Array ( [0] => 2 [1] => 12 [2] => Dinkel Spezial [3] => 0.2 )
[2] => Array ( [0] => 1 [1] => 1 [2] => Essener [3] => 1 )
)
and I use json_encode and echo it, I get this:
[["1","12","Essener","1"],["2","12","Dinkel Spezial","0.2"],["1","1","Essener","1"]]
which is good for me.
Now I have an array with stdClass Objects, which I wasn't able to transform into JSON with json_encode. When I echo it, it just doesn't show anything.
Then I transformed this array with objects to an array like this (with get_object_vars()):
Array (
[0] => Array (
[item_id] => 1
[item_name] => Essener
)
[1] => Array (
[item_id] => 2
[item_name] => Dinkel Spezial
)
[2] => Array (
[item_id] => 3
[item_name] => Saatenbrot
)
)
and when I use json_encode and echo it still doesn't show anything. Can anyone tell me what I am doing wrong or what I need to do to get a JSON array?
I need this json array to send data to an IOS App.
From the documentation:
Note:
In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error.
So you can try to detect the exact error by yourself. From your information I can't see any error.
Furthermore I don't think, it doesn't return anything. Try to var_dump() the result of json_encode(). I assume it returns false, which means an error occured.
So if anybody wondered what was wrong,
the problem was that i had "ä,ü,ö,ß" in my array and i needed to convert these to unicode and then everything worked just fine.

How can I loop through a stdObject's array without raising PHP notices/warnings?

I have the following stdObject obtained thru cURL / json_decode():
stdClass Object
(
[response] => stdClass Object
(
[status] => OK
[num_elements] => 1030
[start_element] => 0
[results] => stdClass Object
(
[publisher] => stdClass Object
(
[num_elements] => 1030
[results] => Array
(
[0] => stdClass Object
(
[id] => 1234
[weight] => 4444
[name] => Pub 1
[member_id] => 1
[state] => active
[code] =>
)
[1] => stdClass Object
(
[id] => 1235
[weight] => 4444
[name] => Pub 2
[member_id] => 2
[state] => active
[code] =>
)
)
)
)
[dbg_info] => stdClass Object
(
[instance] => instance1.server.com
[slave_hit] => 1
[db] => db1.server.com
[reads] => 3
[read_limit] => 100
[read_limit_seconds] => 60
[writes] => 0
[write_limit] => 60
[write_limit_seconds] => 60
[awesomesauce_cache_used] =>
[count_cache_used] =>
[warnings] => Array
(
)
[time] => 70.440053939819
[start_microtime] => 1380833763.4083
[version] => 1.14
[slave_lag] => 0
[member_last_modified_age] => 2083072
)
)
)
I'm looping through it in order to obtain each result's ID:
foreach ($result->response->results->publisher->results as $object) {
$publishers .= $object->id.",";
}
And although the code is working fine, PHP is rising the following notices/warnings:
PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1
PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1
PHP Warning: Invalid argument supplied for foreach() in /var/www/vhosts/domain.net/script.php on line 1
Any ideas? Thanks in advance!
Is it possible you have set the second parameter $assoc in json_decode() to true? Like this:
$result = json_decode($json_from_curl, true);
Thereby, getting back $result with all stdClass Objects converted to associative array.
EDIT:
If the $result you are getting is indeed an associative array, then we should treat it as such. Try replacing your foreach with this:
foreach ($result['response']['results']['publisher']['results'] as $arr) {
$publishers .= $arr['id'] . ",";
}
EDIT2:
From my own testing based on your code, everything should be working correctly. The notices/warnings should not occur. Perhaps some other code not shown in your question is causing it.

How Do I access Data in Facebook Array with PHP?

I have made a simple facebook register widget.
On register the App sends the data to send.php
On send.php I have
print_r($response);
And I get something like this:
Array
(
[algorithm] => HMAC-SHA256
[expires] => 1367953200
[issued_at] => 1367946138
[oauth_token] => BAAE0refKufgBAORkK7hUaVpF8MnFygoqHAHrO3nRJMyNjvJx6RjiMjoqbz2YlfqeogcIPGJJaIgD0xtxhBj1WRgQ5F5SidjwM7ZCKOyZBlEuatIqIccbjGj2uMV5hqtKtZA1g7hOEqMeZAEwmnO6SgRgsb9ittKZCDnPfoxYxCxZAZBAhIKX457IG5ZB4yknv9FZB8QUG7Pt0mfBRQUYG12KoTmO7QRH20LP65FyPqTi7mAZDZD
[registration] => Array
(
[name] => derp derp
[email] => ddddd#gmail.com
[location] => Array
(
[name] => Vienna, Austria
[id] => 1.1116511224109E+14
)
[gender] => male
[phone] => sss
)
[registration_metadata] => Array
(
[fields] => [ {'name':'name'}, {'name':'email'}, {'name':'location'}, {'name':'gender'}, {'name':'phone', 'description':'Phone Number', 'type':'text'},]
)
[user] => Array
(
[country] => at
[locale] => en_US
)
[user_id] => 100000506481284
)
So the data gets passed without a problem but how can I access a specific value?
For example text book array stuff doesnt appear to be working like:
print $response[0];
Gives me an error:
Notice: Undefined offset: 0 in C:\xampp\htdocs\ddd\send.php on line 38
How else can I access the data?
For example how could I store country, email, name or whatever in their own variables so I can echo them later? Its there but I cant seem to figure out how to "digest" it properly with php.
There is no index 0 in the array you pasted..
Use it like
$email = $response['registration']['email'];
If it was an object instead of an array you would do
$email = $response->registration->email
This is very basic PHP array handling.
http://php.net/manual/en/language.types.array.php

Issue getting info out of array

I'm trying to get info out of this information:
Array (
[result] => success
[totalresults] => 1
[startnumber] => 0
[numreturned] => 1
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
[deptid] => 1
[userid] => 39
[name] => Mark Lønquist
[email] => mark.loenquist#outlook.com
[cc] =>
[c] => 79rzVBeJ
[date] => 2013-04-25 16:14:24
[subject] => test
[status] => Open
[priority] => Medium
[admin] =>
[attachment] =>
[lastreply] => 2013-04-25 16:14:24
[flag] => 0
[service] =>
)
)
)
)
The results are printed using:
print_r($results);
Usually, I've been able to do a simple:
$var = $results['something'];
To get it out, but it wont work with this :( Any help is appreciated.
After reformatting the array you pasted, it becomes clear that some elements are nested several levels deep. (It's a "multidimensional array"; see example #6 in the docs.) In those cases, you have to add additional brackets containing each successive key to reach the depth you want. For example, a sample from your $results array:
Array (
[result] => success
[totalresults] => 1
...
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
...
)
)
)
)
You simply need to do $results['totalresults'] to access "totalresults", but to get "tid" you would need to use $results['tickets']['ticket'][0]['tid'].
If you want to get "tid" from all of the tickets when there are multiple, you will have to iterate (loop) over the array of tickets. Probably something like this (untested, but should be close enough for you to figure out):
foreach ($results['tickets']['ticket'] as $ticket) {
echo $ticket['tid'];
}
To see what the problem is with your print_r() you may add error_reporting(E_ALL); to the top of your code.
Note that if you want to retrieve the value for a key such as 'totalresults' then $results['totalresults'] would be sufficient.
However, if you want to get a key from one of the nested arrays such as email then you would have to use $results['result']['tickets']['ticket'][0]['email'].

Categories