Access variables from print_r in php - php

I got a print_r($result) output as follows:
stdClass Object (
[balance] => 998
[batch_id] => 243941208
[cost] => 1
[num_messages] => 1
[message] => stdClass Object (
[num_parts] => 1
[sender] => TMTLCO
[content] => #U0D070D240D4D00200D120D300D4100200D1F0D460D380D4D0D310D4D0D310D4D002000200D060D230D4D
)
[receipt_url] =>
[custom] =>
[messages] => Array (
[0] => stdClass Object (
[id] => 117250619
[recipient] => XXXXXXXX
)
)
[status] => success
)
I could echo the following and get correct output:
echo $result->balance;
echo $result->num_messages;
But the following is not working??
echo $result->message->sender;
echo $result->messages->recipient;
echo $result->messages->status;
What am i doing wrong ??

Miss the 0 which is an array. For accessing recipient you need to add one more Dimension into your $result array.
Messages has a one more array which you miss to navigate. So try with this,
echo $result->message->sender;
echo $result->messages[0]->recipient;
echo $result->status;

Related

Get value of array stdClass Object

I am performing an insertion of values ​​using an api.
then, I send the values ​​correctly and get the return on the same page.
Eg:
<?php
...
$return= $set->createCharge([
'description' => 'Strings etc...',
'amount' => 10.0
)];
print_r($return);
?>
This print_r Result is:
stdClass Object
(
[data] => stdClass Object
(
[charges] => Array
(
[0] => stdClass Object
(
[code] => 555
[reference] => 66
[dueDate] => 23/11/2020
[checkoutUrl] => https://url/number_return
[link] => https://url_return
[installmentLink] => https://url
[payNumber] => 9999999999
[billetDetails] => stdClass Object
(
[bankAccount] => 888888
[ourNumber] => 12121-9
[barcodeNumber] => 0541454141411141414141414
[portfolio] => 0001
)
)
)
)
[success] => 1
)
I tried to get the value '[code']
using the following:
echo $return[0]['code'];//Dont work
echo $return['data']['charges'][0]['code'];//Dont Work
how can I get the value of [code] or another that is in that array?
Each element in the "charges" array is an object. A different syntax is used to refer to the members of the object:
//for an array
echo $someArray['someValue'];
//for an object
echo $someObject->someValue;
echo $someObject->getSomeValue()
Therefore, this is the way for you:
echo $return->data->charges[0]->code;
Or, step by step:
var_dump($return);
var_dump($return->data);
var_dump($return->data->charges);
var_dump($return->data->charges[0]);
var_dump($return->data->charges[0]->code);
var_dump($return->data->charges[0]->code->billetDetails);
var_dump($return->data->charges[0]->code->billetDetails->bankAccount);

PHP - loop through messages in array

I have weird problem with my PHP code. I decoded an elasticsearch json, and I got this array:
Array ( [took] => 1 [timed_out] => [_shards] => Array ( [total] => 6 [successful] => 6 [skipped] => 0 [failed] => 0 ) [hits] => Array ( [total] => 7336 [max_score] => 0.8790647 [hits] => Array ( [0] => Array ( [_index] => logstash-2019.02.25 [_type] => doc [_id] => 5T8GJWkBAZbF3w3t4NF2 [_score] => 0.8790647 [_source] => Array ( [#version] => 1 [log-level] => ERROR [port] => 50906 [host] => 6b14cd1f183d.mynetwork [#timestamp] => 2019-02-25T14:20:01.367Z [Timestamp] => 2019-02-25T13:57:40+0000 [message] => Array ( [0] => 2019-02-25T13:57:40+0000 ERROR something happened in this execution. [1] => something happened in this execution. ) ) ) [1] => Array ( [_index] => logstash-2019.02.25 [_type] => doc [_id] => 7z8GJWkBAZbF3w3t4NF2 [_score] => 0.8790647 [_source] => Array ( [#version] => 1 [log-level] => INFO [port] => 50906 [host] => 6b14cd1f183d.mynetwork [#timestamp] => 2019-02-25T14:20:01.369Z [Timestamp] => 2019-02-25T14:00:13+0000 [message] => Array ( [0] => 2019-02-25T14:00:13+0000 INFO takes the value and converts it to string. [1] => takes the value and converts it to string. ) ) ) ) ) )
What I want is only to print the "messages".
If I run this code:
$echo json_decoded['hits']['hits']['0']['_source']['message']['0']
It echo "2019-02-25T13:57:40+0000 ERROR something happened in this execution."
And if I run this code:
$echo json_decoded['hits']['hits']['1']['_source']['message']['0']
It echo "2019-02-25T14:00:13+0000 INFO takes the value and converts it to string."
ALL GOOD SO FAR, but now I want to run it as loop, to print the messages. I made the following loop:
foreach($json_decoded['hits']['hits'] as $host) {
echo $host[$index]['_source']['message']['0'];
$index++;
}
I don't get any output. What is the problem?
When you foreach over ['hits']['hits'] this will iterate over the next level of the array (so the ['0'] and ['1'] elements). So you don't need to add in the [$index] part of your echo or the $index value...
foreach($json_decoded['hits']['hits'] as $host) {
echo $host['_source']['message']['0'];
}

How to get value out of this...?

Can someone explain me how to get data out of this...like if I just want subject, description..etc...
stdClass Object
(
[tickets] => Array
(
[0] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/1.json
[id] => 1
[external_id] =>
[via] => stdClass Object
(
[channel] => sample_ticket
[source] => stdClass Object
(
[from] => stdClass Object
(
)
[to] => stdClass Object
(
)
[rel] =>
)
)
[created_at] => 2015-04-22T08:30:29Z
[updated_at] => 2015-05-19T06:01:22Z
[type] => incident
[subject] => This is a sample ticket requested and submitted by you
[raw_subject] => This is a sample ticket requested and submitted by you
[description] => This is the first comment. Feel free to delete this sample ticket.
[priority] => high
[status] => closed
[recipient] =>
[requester_id] => 794599791
[submitter_id] => 794599791
[assignee_id] => 794599791
[organization_id] => 39742491
[group_id] => 24344491
[collaborator_ids] => Array
(
)
[forum_topic_id] =>
[problem_id] =>
[has_incidents] =>
[due_at] =>
[tags] => Array
(
[0] => sample
[1] => zendesk
)
[custom_fields] => Array
(
)
[satisfaction_rating] =>
[sharing_agreement_ids] => Array
(
)
[fields] => Array
(
)
[followup_ids] => Array
(
)
[brand_id] => 565681
)
[1] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/10.json
[id] => 10 //multiple object like [0]...
Thanks...Any help would be great..
When you need to access to array's key, use []. When you have object, use ->.
echo $obj->tickets[0]->subject; // returns first subject
echo $obj->tickets[0]->description; // returns first description
You can put it into foreach loop, of course to gain all subjects, etc.
It's STD object so use properties
$obj->tickets[0]->subject
$obj->tickets[0]->description
You can obviously loop tickets
foreach($obj->tickets as $ticket)
{
echo $ticket->subject;
echo $ticket->description
}
this is an std object.to get subject and description follow this
$obj->tickets[0]->subject;
$obj->tickets[0]->description;
if you feel better in array just make it array using this code
$array = get_object_vars($obj);

how to manage json data in php

I called JSON data and got following output:
stdClass Object
(
[request] => stdClass Object
(
[Target] => Affiliate_Report
[Format] => json
[Service] => HasOffers
[Version] => 3
[Method] => getConversions
[api_key] => my_key
[NetworkId] => icubes
[fields] => Array
(
[0] => Offer.name
[1] => Browser.display_name
[2] => Stat.payout
[3] => Stat.sale_amount
[4] => Stat.status
[5] => Stat.datetime
[6] => Stat.ip
[7] => Stat.ad_id
[8] => Stat.affiliate_info1
)
)
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => stdClass Object
(
[page] => 1
[current] => 100
[count] => 75
[pageCount] => 1
[data] => Array
(
[0] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Chrome
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 954.00000
[status] => rejected
[datetime] => 2014-03-31 19:49:50
[ip] => 103.226.84.249
[ad_id] => 102ecc5fe230883c195d8a0e84ef7f
[affiliate_info1] =>
)
)
[1] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 270.00000
[sale_amount] => 545.00000
[status] => approved
[datetime] => 2014-04-18 12:00:20
[ip] => 27.0.51.119
[ad_id] => 10256740541d68b117955aa58529a6
[affiliate_info1] =>
)
)
I want to display that data in tabular form using table tag and also want to insert that data into a MySQL database.
But I dont understand the array format of data.
My code:
$result = file_get_contents($base);
$obj = json_decode($result);
echo"<table>";
foreach($obj as $item) {
echo"
<tr>
<td>$item['Offer.name']</td>
<td>$item['Browser.display_name']</td>
<td>$item['Stat.payout']</td>
<td>$item['Stat.sale_amount']</td>
<td>$item['Stat.datetime']</td>
<td>$item['Stat.ip']</td>
</tr>
";
}
echo"</table>";
This code gives me a blank output.
i used following code and got output but some errors are still there.
$result = file_get_contents($base);
$obj = json_decode($result, true);
echo"<table border=1>";
$i = 0;
foreach($obj['response'] as $item) {
for($i=0;$i<=10;$i++)
{
echo"
<tr>
<td>{$item['data'][$i]['Offer']['name']}</td>
<td>{$item['data'][$i]['Stat']['payout']}</td>
<td>{$item['data'][$i]['Stat']['sale_amount']}</td>
<td>{$item['data'][$i]['Stat']['status']}</td>
<td>{$item['data'][$i]['Stat']['datetime']}</td>
<td>{$item['data'][$i]['Stat']['ip']}</td>
</tr>
";
}
}
echo"</table>";
my output:
why upper rows are displaying blank values when there is no blank data is present?
and what are those errors?
You're using the wrong syntax. It's difficult to see exactly the results you're expecting, the values of the objects being returned look like they are objects of the original json themselves and without any useful data. However, to clarify for you:
You're returning the json_decode() results as an object and trying to access it as an array.
To return an associative array you need to add the 'assoc' parameter to the function, to read:
json_decode($result, true)
and you were trying to eco out the value directly from the associative array, you need to access it via the key, which will give you the value to read:
$result = file_get_contents($base);
$obj = json_decode($result, true);
echo"<table>";
foreach($obj['request'] as $item) {
echo"
<tr>
<td>{$item['fields'][0]}</td>
<td>{$item['fields'][1]}</td>
<td>{$item['fields'][2]}</td>
<td>{$item['fields'][3]}</td>
<td>{$item['fields'][4]}</td>
<td>{$item['fields'][5]}</td>
</tr>
";
}
echo"</table>";
Also, I've added the curly brackets inside the string to escape the variable expressions.
Since you're getting objects in your response, first try json_decode($result->request) and generally don't use [ ] as you normally would as if your result was array
EDIT: Try outputting the response like this:
echo '<pre>';
print_r($your-variable-here);
echo '</pre>';
Then let me know of the output

How to echo a specific value from an array in php using REST API , need to echo name from the given array

Array
(
[root] => Array
(
[status] => Array
(
[statusCode] => 200
[message] => OK
)
[response] => Array
(
[success] => 1
[result] => Array
(
[0] => Array
(
[id] => 1
[content] => 12
[enabled] => 1
[date_modified] => 2013-03-07 19:28:19
[name] => Home page
[category_id] => 1
)
)
[error_messages] =>
[debug] =>
Please help me to echo the name from the given array. i have tried many things but not able to find correct , please help me on it.
Chances are you're going to get more than one name, so:
foreach($yourArrayRef['root']['response']['result'] as $result) {
echo $result['name'];
}
Try this:
$arr['response']['result'][0]['name']
Try this
echo $response["result"][0]["name"];

Categories