How to produce a line space in arrays [duplicate] - php

This question already has answers here:
Pretty-Printing JSON with PHP
(27 answers)
Closed 8 months ago.
$arr = array(
'toemail'=>$v->agent_primary_email,
'agentname'=>$v->agent_firstname,
'agentid'=>$v->agent_id,
'subject'=>'The details of total number of properties saved by your clients',
'totalprop'=>$v->prop_count
);
echo json_encode($arr);exit;
The output looks like this
{"toemail":"abc#gmail.com","agentname":"john","agentid":"110012","subject":"The details of total number of properties saved by your clients","totalprop":"131"}
But what changes should i have make, so that the output looks like this
{"toemail":"abc#gmail.com",
"agentname":"john",
"agentid":"110012",
"subject":"The details of total number of properties saved by your clients",
"totalprop":"131"}

Use JSON_PRETTY_PRINT and also need to use echo "<pre>";
From PHP Manual: Use whitespace in returned data to format it. Available since PHP 5.4.0
$array = array(
'test'=>1,
'test2'=>'test',
'test3'=>'test 3'
);
echo "<pre>";
echo json_encode($array,JSON_PRETTY_PRINT);
Result:
{
"test": 1,
"test2": "test",
"test3": "test 3"
}

Related

Get an item from inside json with php code [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 4 months ago.
I can show the FactorDate item with the following code.
$response=json_decode($response->getBody());
foreach($response as $product)
{
echo $product->FactorDate;
}
but I can not show the Price item. please guide me.
[
{
"CustomerCode": 101,
"FactorNumber": 53,
"FactorDate": 14010201,
"FactorDetails": [
{
"ProductCode": 21901,
"Count": 15,
"Price": 96000000,
"VisitorID": 0
}
]
}
]
The JSON that you had provided looks like price is a part of an array within FactorDetails. Meaning that if you want to get the price you will likely have to do something like:
$product->FactorDetails[0]->Price
Or put into your example, something like this will display both Date and Price:
$response=json_decode($response->getBody());
foreach($response as $product)
{
echo $product->FactorDate;
echo $product->FactorDetails[0]->Price;
}
That should work.
For future reference, you could use a tool like this JSON viewer to see a more readable version of your JSON which can be super helpful when dealing with formatting issues like this one:
https://codebeautify.org/jsonviewer

How to select only one of the objects in a json file [duplicate]

This question already has answers here:
PHP. Is it possible to use array_column with an array of objects
(5 answers)
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 1 year ago.
Hi I'm currently trying to do an API request. The API sends out a json request like this:
[
{
"name": "Test 1"
"yes-or-no": "yes"
},
{
"name": "Test 2"
"yes-or-no": "no"
}
]
My question is, how do I select one of the yes-or-no to echo in the website? I tried doing this:
<?php
$status = json_decode(file_get_contents('url to the json file'));
// Display message AKA uptime.
foreach ($status->yes-or-no as $answer) {
echo $answer.'<br />';
}
?>
But didn't work.
I'm sorry if I got some terms wrong, since I'm pretty new to coding APIs like this.
EDIT: Please see the answer below. It works but now my question is: How do I only display one of them? Instead of both of them displaying at the same time.
I'm not really sure what you are trying to do, but maybe i can shed some light into the question:
$status = json_decode(file_get_contents('url to the json file'), true);
Add ", true" this will make your $status an array instead of an object.
foreach ($status as $answer) {
echo $answer['yes-or-no'].'<br />'; //output yes or no
echo $answer['name'].'<br />'; //output test 1 or test 2
}
Try something like this:
<?php
$statuses = json_decode(file_get_contents('url to the json file'));
foreach ($statuses as $status) {
echo $status->{'yes-or-no'};
}
?>

I want dont print "" in php json [duplicate]

This question already has answers here:
PHP json_encode encoding numbers as strings
(18 answers)
Closed 2 years ago.
I get data php sql after i print from json its but i want dont print a item in ""
i want dont print id in ""
require('config.php');
$sql="SELECT * FROM file";
$result=$conn->query($sql);
foreach($result as $rows){
$recordfil=array();
$recordfil['id']=$rows['idfile'];
// $recordfil['file']=$rows['filef'];
$nnfile[]=$recordfil;
}
$filegen=$nnfile;
$arrayovp=[
'ovpn_file'=>
$filegen
];
echo json_encode($arrayovp);
?>
It output :
{"ovpn_file":[{"id":"36"},{"id":"35"}]}
I want only 36 or 35 without ""
PHP supports automatically encoding numeric strings as numbers using the JSON_NUMERIC_CHECK option, which might be easier than explicitly type-casting all relevant fields.
Try
echo json_encode($arrayovp, JSON_NUMERIC_CHECK);
# {"ovpn_file":[{"id":36},{"id":35}]}
Demo here: https://3v4l.org/ANdUM

How to extract a series of variables from string of data in php? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
The following chunk of data is produced by a webpage along with other html data;
"search":{"searchResults":{"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
{"id": //and so on//
"}
]}}
How to extract multiple variables from this string like data like "id", "rating" etc., to be able to print it on another php page?
You can use json_decode to convert JSON to the array and then iterate through the array
$json = '{
"search":
{
"searchResults":
{
"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
]
}
}
}';
$jsonToArray = json_decode($json,true);
foreach($jsonToArray['search']['searchResults']['results'] as $v){
echo $v['id'];
print_r($v['rating']);echo '<br/>';
}

Want to get one value from json result using php [duplicate]

This question already has answers here:
Get data from JSON file with PHP [duplicate]
(3 answers)
Closed 7 years ago.
How can i get ID value only?
like so here: $fo = "11890408";
{"success":true,"result":{"success":[{"id":"11890408","device_id":"17183","message":"okey","status":"pending","send_at":1455046054,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":1455049654,"canceled_at":0,"failed_at":0,"received_at":0,"error":"","created_at":1455046054,"contact":{"id":"2522330","name":"923336458112","number":"923336458112"}}],"fails":[]}}
Convert the json string to a php data structure and then navigate through the data tree of objects and arrays:
$json = '{
"success":true,
"result":{
"success[
{
"id":"11890408",
"device_id":"17183",
"message":"okey",
"status":"pending",
"send_at":1455046054,
"queued_at":0,
"sent_at":0,
"delivered_at":0,
"expires_at":1455049654,
"canceled_at":0,
"failed_at":0,
"received_at":0,
"error":"",
"created_at":1455046054,
"contact":{
"id":"2522330",
"name":"923336458112",
"number":"923336458112"
}
}
],
"fails":[]
}
}';
$data = json_decode($json);
$fo = $data->result->success[0]->id;

Categories