Json data synchronization in database [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I keep json data from database. I want to sync with the name that comes with the post and get the price of the data I sync. How can I do that?
$video_tkn = "3D";
$reklam_json = json_decode($siteayar->reklam_json);
$video = json_decode($reklam_json->video);
$video_arr = array();
foreach ($video as $v) {
if ($v->video_tur==$video_tkn) {
$video_arr[] = $v->fiyat;
}else{
$video_arr[] = 0;
}
}
print_r($video_arr[0]);
//Output : 0
//$reklam_json->video
[
{
"video_tur":"2D",
"fiyat":"20"
},
{
"video_tur":"3D",
"fiyat":"80"
}
]

You can do something like. The problem with the code that you have written is that in the $video_arr[0] it is always writing 0 as per your else condition because $video_tkn= "3D" value is in second index in the array $video
<?php
$video_tkn = "3D";
$reklam_json = json_decode($siteayar->reklam_json);
$video = json_decode($reklam_json->video);
$video_arr = array();
foreach ($video as $v) {
if ($v->video_tur==$video_tkn) {
$video_arr[] = $v->fiyat;
}
}
print_r($video_arr[0]);

Related

Count rows with model::count laravel [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I am trying to count the records in the table, but it's showing me nothing in the response, I am working on an API, can anybody help please ?
public function devicesCount()
{
$x = Device::count();
if ($x) {
return ['devices' => $x];
} else {
return ['message' => 'ranii karrezt ydyyyyyyk'];
}
}
use $x = Device::all()->count(); in place of $x = Device::count();
or use Laravel Aggregates like
$count = DB::table('devices')->count();

How I can extract field to xml in Php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a xml with more subcategory.
I want extract in Php the “long_name” where type is “adminkstrative_area_level_3”
How I can do?
This is my xml https://ibb.co/fY27bJ
I tried but don’t work
<?
$string_data = "https://maps.googleapis.com/maps/api/geocode/xml?latlng=41.51,15.16&key=AIzaSyClG_vc2nkQCzXqvDzW1maPrUWLyADI7xI";
$xml = simplexml_load_string($string_data);
$citta = (string) $xml->result[0]->address_component[3]->long_name;
echo "<p>".$citta."</p>";
?>
You are missing geoname in $xml-> name;
Try it like this:
$xml = simplexml_load_string($string_data);
$citta = (string)$xml->geoname->name;
echo $citta;
Demo Php
If you want to loop through mulitple items you could use:
foreach ($xml->geoname as $item) {
echo $item->name;
}
Update:
For the updated part you could use the same technique:
$xml = simplexml_load_file("https://maps.googleapis.com/maps/api/geocode/xml?latlng=41.51,15.16&key=AIzaSyClG_vc2nkQCzXqvDzW1maPrUWLyADI7xI");
foreach ($xml->result as $item) {
if ((string)$item->type === "administrative_area_level_3") {
echo $item->address_component->long_name;
}
}
Or by index [1]:
echo $xml->result[1]->address_component->long_name;

i want to combine fcm response with another response [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
{
"success":1,"Data":{"status":"message sent",
"message_id":3594,
"personaluid":"161",
"gid":"0",
"uid":"160",
"message":"cxzczxczxsada123",
"chat_image":"",
"created_at":"2017-02-04 16:52:11",
"utype":"1"
}
}
{
aps =
{alert =
{body = hdhd; title=from some person name;};
badge = 1;
};
"gcm.message_id" = "0:1487247942837269%19aca14d19aca14d";
"gcm.notification.Content_available" = 1;
"gcm.notification.created_at" = "2017-02-16 17:55:41";
"gcm.notification.uid" = 161;
}
convert your json data to php array then use array_merge to merge both. Then again convert it to json data. Example:
<?php
$json1='{"success":1,
"Data":{"status":"message sent","message_id":3594,"personaluid":"161","gid":"0","uid":"160","message":"cxzczxczxsada123","chat_image":"","created_at":"2017-02-04 16:52:11","utype":"1"}
}';
$json2='{
"multicast_id":5114850183838817498,
"success":1,
"failure":0,
"canonical_ids":0,
"results":[{"message_id":"0:1487247086404626%19aca14d19aca14d"}]
}';
$ar1=json_decode($json1,true);
$ar2=json_decode($json2,true);
$ar1=array_merge($ar1,$ar2);
echo json_encode($ar1);
output:
{"success":1,"Data":{"status":"message sent","message_id":3594,"personaluid":"161","gid":"0","uid":"160","message":"cxzczxczxsada123","chat_image":"","created_at":"2017-02-04 16:52:11","utype":"1"},"multicast_id":5.1148501838388e+18,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1487247086404626%19aca14d19aca14d"}]}
You could array merge the 2 decoded arrays.
$data = json_decode($result, true);
$data = array_merge($data[0], $data[1]);
$data = json_encode($data, JSON_FORCE_OBJECTS);`

Foreach loop php conversion [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I execute on my main page some php code which includes :
foreach ($fbdata->feed->data as $fbpost)
{
...
}
How can we convert this , into a loop that goes from (i to z)(0 to 10) ?
Simple for loop
for($i = 0; $i < 10; $i++) {
$fbpost = $fbdata->feed->data[$i];
...
}
or if you like to use the as, try using a foreach but slicing the array before using it
$fbPosts = array_slice($fbdata->feed->data, 0, 10);
foreach($fbPosts as $fbpost) {
...
}

parse from json url the high price of a stock from yahoo.finance.historicaldata using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm learning how to parse json file...cant figure out my mistakes..tryin to plot those data on graph
$url="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in(%22TATASTEEL.NS%22)%20and%20startDate%3D%222015-07-01%22%20and%20endDate%3D%222015-07-30%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
$output = file_get_contents($url);
$jdecode=json_decode($output,true);
foreach ($jdecode[query][results][quote][Open] as $key => $value) {
# code...
echo $key." : ".$value."<br>";
}
ex: when i enter this query in yql console:
select * from yahoo.finance.historicaldata where symbol in ("TATASTEEL.NS") and startDate = "2014-03-01" and endDate = "2015-03-30"
i get a json link like this:
'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22TATASTEEL.NS%22)%20and%20startDate%20%3D%20%222014-03-01%22%20and%20endDate%20%3D%20%222015-03-30%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback='
how do i parse this json...
Try this: If you want to still return array of High Stocks
$json = '{"query":{"count":22,"created":"2015-09-08T03:16:02Z","lang":"en-US","diagnostics":{"url":[{"execution-start-time":"0","execution-stop-time":"1","execution-time":"1","content":"http://www.datatables.org/yahoo/finance/yahoo.finance.historicaldata.xml"},{"execution-start-time":"9","execution-stop-time":"31","execution-time":"22","content":"http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS"},{"execution-start-time":"34","execution-stop-time":"45","execution-time":"11","content":"http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS"}],"publiclyCallable":"true","cache":[{"execution-start-time":"8","execution-stop-time":"8","execution-time":"0","method":"GET","type":"MEMCACHED","content":"849df977aa5fea49fe77822dc0412ba5"},{"execution-start-time":"33","execution-stop-time":"34","execution-time":"1","method":"GET","type":"MEMCACHED","content":"28e5f7f76a8c1b5cf5932f92c807c420"}],"query":[{"execution-start-time":"8","execution-stop-time":"33","execution-time":"25","params":"{url=[http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS]}","content":"select * from csv(0,1) where url=#url"},{"execution-start-time":"34","execution-stop-time":"46","execution-time":"12","params":"{columnsNames=[Date,Open,High,Low,Close,Volume,Adj_Close], url=[http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS]}","content":"select * from csv(2,0) where url=#url and columns=#columnsNames"}],"javascript":{"execution-start-time":"6","execution-stop-time":"64","execution-time":"57","instructions-used":"242787","table-name":"yahoo.finance.historicaldata"},"user-time":"65","service-time":"35","build-version":"0.2.154"},"results":{"quote":[{"Symbol":"TATASTEEL.NS","Date":"2015-07-30","Open":"252.90","High":"253.00","Low":"247.55","Close":"248.15","Volume":"5169000","Adj_Close":"248.15"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-29","Open":"251.00","High":"254.65","Low":"249.40","Close":"250.55","Volume":"4003400","Adj_Close":"250.55"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-28","Open":"251.00","High":"252.45","Low":"246.40","Close":"249.25","Volume":"5538500","Adj_Close":"249.25"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-27","Open":"263.00","High":"263.20","Low":"250.05","Close":"250.75","Volume":"7648400","Adj_Close":"250.75"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-24","Open":"269.70","High":"271.65","Low":"264.20","Close":"265.05","Volume":"3810200","Adj_Close":"265.05"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-23","Open":"274.00","High":"274.45","Low":"269.30","Close":"269.90","Volume":"6961200","Adj_Close":"269.90"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-22","Open":"271.25","High":"281.00","Low":"268.70","Close":"280.40","Volume":"5910800","Adj_Close":"272.40"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-21","Open":"284.10","High":"284.10","Low":"273.85","Close":"274.70","Volume":"5095100","Adj_Close":"266.8626"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-20","Open":"285.00","High":"287.70","Low":"283.00","Close":"283.55","Volume":"3733700","Adj_Close":"275.4601"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-17","Open":"282.00","High":"286.35","Low":"280.00","Close":"284.05","Volume":"6449900","Adj_Close":"275.9459"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-16","Open":"285.10","High":"286.50","Low":"280.10","Close":"280.80","Volume":"6313300","Adj_Close":"272.7886"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-15","Open":"282.70","High":"284.80","Low":"279.10","Close":"280.40","Volume":"3679700","Adj_Close":"272.40"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-14","Open":"284.75","High":"286.70","Low":"280.50","Close":"281.15","Volume":"3430700","Adj_Close":"273.1286"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-13","Open":"283.50","High":"285.70","Low":"279.00","Close":"284.45","Volume":"3436800","Adj_Close":"276.3345"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-10","Open":"282.90","High":"284.50","Low":"278.75","Close":"280.90","Volume":"4021300","Adj_Close":"272.8857"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-09","Open":"283.50","High":"287.40","Low":"279.60","Close":"280.50","Volume":"6214700","Adj_Close":"272.4972"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-08","Open":"294.00","High":"294.00","Low":"281.80","Close":"283.15","Volume":"8141900","Adj_Close":"275.0715"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-07","Open":"301.70","High":"303.05","Low":"296.10","Close":"298.20","Volume":"3298700","Adj_Close":"289.6922"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-06","Open":"295.00","High":"299.75","Low":"292.00","Close":"299.00","Volume":"3708100","Adj_Close":"290.4693"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-03","Open":"306.35","High":"307.90","Low":"299.55","Close":"301.20","Volume":"2742500","Adj_Close":"292.6066"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-02","Open":"307.25","High":"310.45","Low":"303.20","Close":"304.35","Volume":"3840100","Adj_Close":"295.6667"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-01","Open":"305.00","High":"309.00","Low":"303.40","Close":"306.95","Volume":"3143800","Adj_Close":"298.1925"}]}}}';
$a = json_decode($json, true);
$high_stocks = [];
foreach($a['query']['results']['quote'] as $key => $val)
{
$high_stocks = $val['High'];
}
print_r($high_stocks);
Or if you want to just echo it.
$json = '{"query":{"count":22,"created":"2015-09-08T03:16:02Z","lang":"en-US","diagnostics":{"url":[{"execution-start-time":"0","execution-stop-time":"1","execution-time":"1","content":"http://www.datatables.org/yahoo/finance/yahoo.finance.historicaldata.xml"},{"execution-start-time":"9","execution-stop-time":"31","execution-time":"22","content":"http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS"},{"execution-start-time":"34","execution-stop-time":"45","execution-time":"11","content":"http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS"}],"publiclyCallable":"true","cache":[{"execution-start-time":"8","execution-stop-time":"8","execution-time":"0","method":"GET","type":"MEMCACHED","content":"849df977aa5fea49fe77822dc0412ba5"},{"execution-start-time":"33","execution-stop-time":"34","execution-time":"1","method":"GET","type":"MEMCACHED","content":"28e5f7f76a8c1b5cf5932f92c807c420"}],"query":[{"execution-start-time":"8","execution-stop-time":"33","execution-time":"25","params":"{url=[http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS]}","content":"select * from csv(0,1) where url=#url"},{"execution-start-time":"34","execution-stop-time":"46","execution-time":"12","params":"{columnsNames=[Date,Open,High,Low,Close,Volume,Adj_Close], url=[http://ichart.finance.yahoo.com/table.csv?a=6&b=1&e=30&g=d&c=2015&d=6&f=2015&s=TATASTEEL.NS]}","content":"select * from csv(2,0) where url=#url and columns=#columnsNames"}],"javascript":{"execution-start-time":"6","execution-stop-time":"64","execution-time":"57","instructions-used":"242787","table-name":"yahoo.finance.historicaldata"},"user-time":"65","service-time":"35","build-version":"0.2.154"},"results":{"quote":[{"Symbol":"TATASTEEL.NS","Date":"2015-07-30","Open":"252.90","High":"253.00","Low":"247.55","Close":"248.15","Volume":"5169000","Adj_Close":"248.15"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-29","Open":"251.00","High":"254.65","Low":"249.40","Close":"250.55","Volume":"4003400","Adj_Close":"250.55"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-28","Open":"251.00","High":"252.45","Low":"246.40","Close":"249.25","Volume":"5538500","Adj_Close":"249.25"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-27","Open":"263.00","High":"263.20","Low":"250.05","Close":"250.75","Volume":"7648400","Adj_Close":"250.75"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-24","Open":"269.70","High":"271.65","Low":"264.20","Close":"265.05","Volume":"3810200","Adj_Close":"265.05"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-23","Open":"274.00","High":"274.45","Low":"269.30","Close":"269.90","Volume":"6961200","Adj_Close":"269.90"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-22","Open":"271.25","High":"281.00","Low":"268.70","Close":"280.40","Volume":"5910800","Adj_Close":"272.40"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-21","Open":"284.10","High":"284.10","Low":"273.85","Close":"274.70","Volume":"5095100","Adj_Close":"266.8626"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-20","Open":"285.00","High":"287.70","Low":"283.00","Close":"283.55","Volume":"3733700","Adj_Close":"275.4601"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-17","Open":"282.00","High":"286.35","Low":"280.00","Close":"284.05","Volume":"6449900","Adj_Close":"275.9459"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-16","Open":"285.10","High":"286.50","Low":"280.10","Close":"280.80","Volume":"6313300","Adj_Close":"272.7886"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-15","Open":"282.70","High":"284.80","Low":"279.10","Close":"280.40","Volume":"3679700","Adj_Close":"272.40"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-14","Open":"284.75","High":"286.70","Low":"280.50","Close":"281.15","Volume":"3430700","Adj_Close":"273.1286"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-13","Open":"283.50","High":"285.70","Low":"279.00","Close":"284.45","Volume":"3436800","Adj_Close":"276.3345"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-10","Open":"282.90","High":"284.50","Low":"278.75","Close":"280.90","Volume":"4021300","Adj_Close":"272.8857"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-09","Open":"283.50","High":"287.40","Low":"279.60","Close":"280.50","Volume":"6214700","Adj_Close":"272.4972"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-08","Open":"294.00","High":"294.00","Low":"281.80","Close":"283.15","Volume":"8141900","Adj_Close":"275.0715"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-07","Open":"301.70","High":"303.05","Low":"296.10","Close":"298.20","Volume":"3298700","Adj_Close":"289.6922"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-06","Open":"295.00","High":"299.75","Low":"292.00","Close":"299.00","Volume":"3708100","Adj_Close":"290.4693"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-03","Open":"306.35","High":"307.90","Low":"299.55","Close":"301.20","Volume":"2742500","Adj_Close":"292.6066"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-02","Open":"307.25","High":"310.45","Low":"303.20","Close":"304.35","Volume":"3840100","Adj_Close":"295.6667"},{"Symbol":"TATASTEEL.NS","Date":"2015-07-01","Open":"305.00","High":"309.00","Low":"303.40","Close":"306.95","Volume":"3143800","Adj_Close":"298.1925"}]}}}';
$a = json_decode($json, true);
foreach($a['query']['results']['quote'] as $key => $val)
{
echo 'High Stocks: ' . $val['High'] . '<br/>';
}

Categories