Hi I encountered a problem. I will record the data I received from the database.
JSON=>
{
"success": true,
"timestamp": 1565251506,
"base": "EUR",
"date": "2019-08-08",
"rates": {
"AED": 4.119657,
"AFN": 87.689574,
"ALL": 121.192477,
"AMD": 533.113395,
"ANG": 1.998509,
"AOA": 398.760307,
"ARS": 51.036305,
"AUD": 1.654423
}
}
After Json decode
array:5 [
"success" => true
"timestamp" => 1565205306
"base" => "EUR"
"date" => "2019-08-07"
"rates" => array:168 [
"AED" => 4.118588
"AFN" => 87.74397
"ALL" => 121.002609
"AMD" => 534.279745
"ANG" => 2.001014
]
]
I want this=> But How do I get quote and rate?
$response = file_get_contents("rate.json");
$datas = json_decode($response, true);
foreach ($datas as $data) {
$rates = new Rate();
$rates->base = $datas['base'];
$rates->quote = 'AED';
$rates->rate = '4.119657';
$rates->save();
}
You're looping over the wrong thing here, you should be looping over the rates and saving the corresponding key and value:
$response = file_get_contents("rate.json");
$data = json_decode($response, true);
foreach ($data['rates'] as $quote => $rate) {
$rates = new Rate();
$rates->base = $data['base'];
$rates->quote = $quote;
$rates->rate = $rate;
$rates->save();
}
Related
I made json code using json encode, the results are correct, but I want to add a satatus response to the object. How to ?
this my code
public function get(){
header('Content-Type: application/json');
$db = $this->M_order->db_order();
$response = array();
$data = array();
foreach ($db as $key) {
$data[] = array(
'id' => $key->id_user,
'name' => $key->name,
'destination' =>$key->destination
);
}
$response['data'] = $data;
echo json_encode($response, TRUE);
}
this result my json
{
"data": [
{
"id": "1",
"name": "amar",
"destination": "USA"
}
]
}
here I want to add a status header in object, like the following ...
{
"status": 200,
"error": false,
"data": [
{
"id": "1",
"name": "amar",
"destination": "USA"
},
]
}
how to create ?
As I understand your question,
$response = array();
$data = array();
foreach ($db as $key) {
$data[] = array(
'id' => $key->id_user,
'name' => $key->name,
'destination' =>$key->destination
);
}
$response['status'] = 200;
$response['error'] = false;
$response['data'] = $data;
I have a table with 3 fields that I query and put the values in a JSON. The table has hostname to application name and application id. I currently have a perl script that outputs the following json string.
[
{
"app_id" : "1234",
"app_name" : "Managed File Transfer",
"ci_name" : "hosta7"
},
{
"app_name" : "Patrtol",
"app_id" : "1235",
"ci_name" : "hosta7"
},
{
"app_id" : "1236",
"app_name" : "RELATIONAL DATA WAREHOUSE",
"ci_name" : "hosta7"
},
{
"ci_name" : "hosta7",
"app_id" : "1237",
"app_name" : "Managed File Transfer"
},
{
"app_id" : "1238",
"app_name" : "Initio Application",
"ci_name" : "hosta7"
},
{
"app_id" : "1239",
"app_name" : "Data Warehouse Operations Infrastructure",
"ci_name" : "hosta7"
},
{
"app_id" : "2345",
"app_name" : "Tableou",
"ci_name" : "hostb"
}
]
I want the resulting json string like the following where if the ci_name already exists, I want the new item to be added to the current entry of the host in the JSON string. So essentially, I want this JSON string
{
"hosts" : [{
"hosta" :[
{
"app_id": "1234",
"app_name": "Managed File Transfer"
},
{
"app_id": "1235",
"app_name": "Patrol"
},
{
"app_id": "1236",
"app_name": "RELATIONAL DATA WAREHOUSE"
},
{
"app_id": "1237",
"app_name": "Managed File Transfer"
},
{
"app_id": "1238",
"app_name": "Initio Application"
},
{
"app_id": "1239",
"app_name": "Data Warehouse Operations Infrastructure"
}
],
"hostb" : [
{
"app_id": "2345",
"app_name": "Tableou"
}
]
}]
}
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
my $hosts = [
{
'app_id' => '1234',
'app_name' => 'Managed File Transfer',
'ci_name' => 'hosta7'
},
{
'app_name' => 'Patrtol',
'app_id' => '1235',
'ci_name' => 'hosta7'
},
{
'app_id' => '1236',
'app_name' => 'RELATIONAL DATA WAREHOUSE',
'ci_name' => 'hosta7'
},
{
'ci_name' => 'hosta7',
'app_id' => '1237',
'app_name' => 'Managed File Transfer'
},
{
'app_id' => '1238',
'app_name' => 'Initio Application',
'ci_name' => 'hosta7'
},
{
'app_id' => '1239',
'app_name' => 'Data Warehouse Operations Infrastructure',
'ci_name' => 'hosta7'
},
{
'app_id' => '2345',
'app_name' => 'Tableou',
'ci_name' => 'hostb'
}
];
my $output;
foreach my $row (#$hosts) {
push #$output, $row;
}
my $json = new JSON;
$json->pretty(1);
print $json->encode($output);
You don't want to push directly, you want to push under a key taken from the ci_name, and you only want to copy the app id and name.
for my $element (#$hosts) {
push #{ $output->{ $element->{ci_name} } },
{ map { $_ => $element->{$_} } qw( app_id app_name ) };
}
Probably the code will look like following snippet
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use Data::Dumper;
my $debug = 0;
my %data;
while( <DATA> ) {
chomp;
next if /app_id/;
my ($app_id,$ci_name,$app_name) = split /,/;
push #{$data{hosts}{$ci_name}}, {app_id => $app_id, app_name => $app_name };
}
print Dumper(\%data) if $debug;
my $json = encode_json \%data;
print $json;
__DATA__
app_id,ci_name,app_name
1234,hosta7,Managed File Transfer
1235,hosta7,Patrtol
1236,hosta7,RELATIONAL DATA WAREHOUSE
1237,hosta7,Managed File Transfer
1238,hosta7,Initio Application
1239,hosta7,Data Warehouse Operations Infrastructure
2345,hostb,Tableou
I'm trying to write to a slide element.
I keep getting the error responses of "Invalid JSON payload received. Unknown name" and " Cannot bind query parameter. Field"
My request is
{
"requests": [
{
"deleteText": {
"objectId": "g33c4ea3b90_0_9",
"textRange": {
"type": "FROM_START_INDEX",
"startIndex": 0
}
}
},
{
"insertText": {
"objectId": "g33c4ea3b90_0_9",
"text": "$8.23",
"insertionIndex": 0
}
}
]
}
I am posting to: https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate?access_token=ACCESS_TOKEN
I am manually cURLing the request in PHP:
$params = '
{
"requests": [
{
"deleteText": {
"objectId": "'.$page_object_id.'",
"textRange": {
"type": "FROM_START_INDEX",
"startIndex": 0
}
}
},
{
"insertText": {
"objectId": "'.$page_object_id.'",
"text": "$8.23",
"insertionIndex": 0
}
}
]
}
';
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
CURLOPT_CUSTOMREQUEST => 'POST' ,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false ,
CURLINFO_HEADER_OUT => true ,
CURLOPT_VERBOSE => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
$obj = json_decode($content, true);
print_r( $obj );
When I call it from the API Explorer it works perfectly, but not from my server.
So the problem was the use of double quotes instead of single quotes in the request parameter json string.
So the following works:
{
'requests': [
{
'deleteText': {
'objectId': 'g33c4ea3b90_0_9',
'textRange': {
'type': 'FROM_START_INDEX',
'startIndex': 0
}
}
},
{
'insertText': {
'objectId': 'g33c4ea3b90_0_9',
'text': '$44.23',
'insertionIndex': 0
}
}
]
}
I had been working on this for two days.
Sorry guys my title might not really explain what i want to achieve, i have an array of countries and i want to save it into the data base below is my code
public function actionCountries(){
$countries = array("AF" => "Afghanistan",
"AX" => "Ă…land Islands",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AO" => "Angola",
"AI" => "Anguilla",
"AQ" => "Antarctica",
"AG" => "Antigua and Barbuda",
"AR" => "Argentina",
"AM" => "Armenia",
"AW" => "Aruba",
"AU" => "Australia",
"AT" => "Austria",
"AZ" => "Azerbaijan",
"BS" => "Bahamas",
"BH" => "Bahrain",
"BD" => "Bangladesh",
"BB" => "Barbados",
"YE" => "Yemen",
"ZM" => "Zambia",
"ZW" => "Zimbabwe");
$model = new Country();
foreach($countries as $code => $name){
$model->code = $code
$model->code = $name
if($model->save()){
echo $model->countryId;
}
}
}
i want to save all of the country into the database on one single click, but the result i got is empty, if i supply a false argument to the save() method i got the last element of the array saved into the database like below
$model = new Country();
foreach($countries as $code => $name){
$model->code = $code
$model->code = $name
if($model->save(false)){
echo $model->countryId;
}
}
this code will save only the last element in the array while the first code did nothing
Any help on this thanks
It's because
$model = new Country();
is outside of the loop so you are saving the same model over and over again. Move this inside the loop.
By the way - it's not efficient. Take a look at batchInsert() method for more memory-friendly way to do it.
Can someone pls help me on how to array_push to a multidimensional array.
This is my code for pushing.
$arrDataChart3["categories"] = array();
array_push($arrDataChart3["categories"], array(
"category"=>array ()
)
);
while($row = sqlsrv_fetch_array($myAcc2)) {
array_push($arrDataChart3["categories"]["category"], array(
"label" => $row["position"])
);
}
i want to push $row["position"] to "category" from my database.
Here's what the array should contain.
"categories": [
{
"category": [
{
"label": "Q1"
},
{
"label": "Q2"
},
{
"label": "Q3"
},
{
"label": "Q4"
}
]
}
],
Thanks in advance
i used this solved the problem
$arrDataChart3["categories"] = array();
$arr=array ();
while($row = sqlsrv_fetch_array($myAcc2)) {
array_push($arr, array(
"label"=>$row['position']
)
);
}
array_push($arrDataChart3["categories"], array(
"category"=>$arr
)
);