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
Related
I'm trying to get the values from [iso_3166_1] and [title] in an array, which is inside another array.
I have searched and tried several solutions and tips, but none of them works, and now I'm stuck.
The content is fetched like this
$content = json_decode($jsonFile);
Then I have the following content
stdClass Object (
[id] => 508947 [titles] => Array (
[0] => stdClass Object ( [iso_3166_1] => FR [title] => Devenir rouge [type] => )
[1] => stdClass Object ( [iso_3166_1] => MX [title] => Red [type] => )
[2] => stdClass Object ( [iso_3166_1] => LV [title] => Es sarkstu [type] => )
[3] => stdClass Object ( [iso_3166_1] => NO [title] => Rød [type] => )
[4] => stdClass Object ( [iso_3166_1] => SE [title] => Röd [type] => )
[5] => stdClass Object ( [iso_3166_1] => PE [title] => Red [type] => )
)
)
I have tried to make a foreach loop like this for instance, but that only gives me Warning: Invalid argument supplied for foreach():
foreach($content as $row) {
foreach($row['titles'] as $country) {
echo $country['iso_3166_1']['title'];
}
}
I have also tried tho following, to try get rid of the stdClass Objects in the array, which does not seem to work either:
$content = json_decode(json_encode($content), true);
$content = (array)$content;
this should work for you to compare & understand why your code fails.
$titles = $content->titles;
foreach ($titles as $country_instance) {
echo $country_instance->iso_3166_1;
echo '-';
echo $country_instance->title;
echo '<br>';
}
If I understand the question right, json_decode associative mode should return arrays of arrays from your JSON.
$array = json_decode($json, true);
You can also try using flag JSON_OBJECT_AS_ARRAY but from what I've read it only does something when null is supplied as the second argument.
I am able to receive the following JSON data with the following PHP code:
$json = file_get_contents('https://xxxx');
$data = json_decode($json,true);
$forex = $data['items'];
echo "<pre>";
print_r($forex);
exit;
This gives me the following JSON data:
Array
(
[0] => Array
(
[new] =>
[data] => Array
(
[direction] => 1
[pip] => 0
[exchange] => FOREX
[symbol] => USDCHF
[interval] => 15
[pattern] => Resistance
[complete] =>
[identified] => 2020-02-26T14:45:00.000Z
[age] => 0
[length] => 259
[found] => 2020-02-26T14:45:56.381Z
[result_type] => KeyLevel
[result_uid] => 642525551
[prediction_price_from] => 0
[prediction_price_to] => 0
[group_name] => FX Majors
[symbol_name] => USDCHF
[symbol_id] => 0
[analysis_text] => Approaching Resistance level of 0.9800 identified at 2/26 14:45. This pattern is still in the process of forming. Possible bullish price movement towards the resistance 0.9800 within the next 15 hours.
[expires_at] => 2020-02-27T05:48:36.701Z
)
[links] => Array
(
[0] => Array
(
[rel] => detail
[href] => https://xxxx
)
[1] => Array
(
[rel] => analysis
[href] => https://xxxx
)
[2] => Array
(
[rel] => chart-xs
[href] => https://xxxx
)
[3] => Array
(
[rel] => chart-sm
[href] => https://xxxx
)
[4] => Array
(
[rel] => chart-md
[href] => https://xxxx
)
[5] => Array
(
[rel] => chart-lg
[href] => https://xxxx
)
[6] => Array
(
[rel] => icon-arrow
[href] => https://xxxx
)
)
)
What I am trying to do is to save the information into my MySQL database, however I am unable to get the data displayed even by testing to echo the data:
foreach($forex as $info) {
echo $info->symbol . '<br>';
}
Anyone with a possible solution for me to save the data or even enable me to display the data for each Array (Example: symbol, pattern, link chart-md).
Below the commented code:
//Save json text into variable $json
$json = file_get_contents('https://xxxx');
//Convert json into PHP array
$data = json_decode($json,true);
//Set $forex as $data['items'];
$forex = $data['items'];
//Print
echo "<pre>";
print_r($forex);
exit;
$forex is a PHP array variable (not a JSON string).
You could save your data into MySQL VARCHAR[Length] field using json string variable $json instead of PHP array $forex.
When you need to get the data from DB and display it, you could get JSON data from MySQL and convert it from JSON to Array using PHP json_decode($json,true).
If you want to use a value into a PHP array, use $info["symbol"] instead of $info->symbol
I'm trying to get data out of some JSON DATA. I'm using the following lines to decode it right now
$json_array = (array)(json_decode($response));
When I print my JSON Decoded array, I have the following data below:
I would like to get the details from the details section, where there is multiple sets of from/to_date's, and up/down numbers. Nothing I seem to do works though to get me to that data. I can print out the data from other areas like usage, but, I can't get into the details.
Array (
[resp_code] => SUCCESS
[caller_ref] => 2017092002282130006180
[server_ref] => 2017092002282169760291
[data] => stdClass Object (
[type] => monthly
[group_id] => 19
[device_id] => 32
[sn] => sn1234
[usages] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 22370
[down] => 119217
[ts] => 2017-09-01T00:00:00
)
)
[details] => stdClass Object (
[3] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 5522
[down] => 40301
[ts] => 2017-09-01T00:00:00
)
)
[2] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 6905
[down] => 32029
[ts] => 2017-09-01T00:00:00
)
)
)
)
)
Whats wrong with objects?
$obj = json_decode($response);
echo $obj->data->details[0]->from_date;
Or to loop it:
foreach ($obj->data->details as $item) {
echo $item->from_date;
// same goes for: to_date, up etc.
}
Simple and sexy!
Update:
It looks as if $item would be an array as well, so if you have problems try:
foreach ($obj->data->details as $item) {
echo $item[0]->from_date;
// same goes for: to_date, up etc.
}
I am trying to access the values of an array.
Array ( [0] => stdClass Object ( [action_type] => comment [value] => 19074.125 ) [1] => stdClass Object ( [action_type] => like [value] => 2179.9 ) [2] => stdClass Object ( [action_type] => link_click [value] => 16.633202528886 ) [3] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_add_to_cart [value] => 451.45857988166 ) [4] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_complete_registration [value] => 897.60588235294 ) [5] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_initiate_checkout [value] => 1230.5887096774 ) [6] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_lead [value] => 372.17804878049 ) [7] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_purchase [value] => 2384.265625 ) [8] => stdClass Object ( [action_type] => offsite_conversion.fb_pixel_view_content [value] => 63.316597510373 ) [9] => stdClass Object ( [action_type] => post [value] => 8477.3888888889 ) [10] => stdClass Object ( [action_type] => post_like [value] => 193.15569620253 ) [11] => stdClass Object ( [action_type] => unlike [value] => 76296.5 ) [12] => stdClass Object ( [action_type] => page_engagement [value] => 15.168290258449 ) [13] => stdClass Object ( [action_type] => post_engagement [value] => 15.274574574575 ) [14] => stdClass Object ( [action_type] => offsite_conversion [value] => 43.39960182025 ) )
I need to access the value of [action_type] => offsite_conversion.fb_pixel_purchase [value] => 2384.265625 , how to get the value of only offsite_conversion.fb_pixel_purchase ??
these values comes from the foreach loop.
$r = $i->cost_per_action_type;
foreach($r as $key => $value){
echo $key["action_type"];
}
the array you are seeing is the print_r($x).
If you want to use the result as an array and not as object, use:
//will return an associative array
$x = json_decode($j, true);
In your code $x is an object, not array, so that foreach is incorrect. You should work on your code and give some reasons for witch you encoded an array just to decode it and try to use it like an array not as object.
You should change the Object to array if you are using the array and got rid of the encode.
You can do what #Chay22 did in the example as array:
foreach($r as $val){
if ($val[action_type] == 'offsite_conversion.fb_pixel_purchase') {
echo $val[value];
}
}
You're trying to treat object as array which doesn't simply working. Use arrow operator -> instead to get object value.
foreach($r as $val){
echo $val->action_type;
}
In case you're really wanted it to be an array, make sure to implement interfaces that gives ability to treat object as an array ArrayAccess, Countable, etc. You can also extends ArrayObject class, too.
And, according to your comment
foreach($r as $val){
if ($val->action_type == 'offsite_conversion.fb_pixel_purchase') {
echo $val->value;
}
}
Here I have an output from a website using Soap
stdClass Object
(
[page] => 0
[items] => 3
[total] => 3
[saleItems] => stdClass Object
(
[saleItem] => Array
(
[0] => stdClass Object
(
[reviewState] => open
[trackingDate] => 2011-11-03T01:06:43.547+01:00
[modifiedDate] => 2011-11-03T01:06:43.677+01:00
[clickDate] => 2011-10-30T22:57:57.383+01:00
[adspace] => stdClass Object
(
[_] => Beslist.nl [id] => 1437603
)
[admedium] => stdClass Object
(
[_] => 001. Program logo
[id] => 535098
)
[program] => stdClass Object
(
[_] => Zavvi NL
[id] => 8991
)
[clickId] => 1565847253976339456
[clickInId] => 0
[amount] => 40.45
[commission] => 2.83
[currency] => EUR
[gpps] => stdClass Object
(
[gpp] => Array
(
[0] => stdClass Object
(
[_] => shoplink
[id] => zpar0
)
)
)
[trackingCategory] => stdClass Object
(
[_] => Default
[id] => 45181
)
[id] => 46a4f84a-ba9a-45b3-af86-da5f3ec29648
)
)
)
)
I want to have the data (with a foreach loop) from program, commission and gpp->_. I can get the data from program and commission like this:
foreach ($sales->saleItems->saleItem as $sale) {
$programma = $sale->program->_;
$commissie = $sale->commission;
}
Works like a charm. However I can't get the data from the gpp->_ (want to have shoplink as result). I currently have:
foreach ($sales->saleItems->saleItem->gpps->gpp as $tracking) {
echo $tracking->_;
}
I get the error "Trying to get property of non-object". I've tried lots if variations and can't get it to work. Think I'm really close. Anyone has a solution?
This should work
foreach ($sales->saleItems->saleItem as $sale) {
foreach($sale->gpps->gpp as $tracking) {
echo $tracking->_;
}
As saleItem is an array, you won't be able to use chaining on it.