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

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

Related

How to get object(stdClass) value in php? [duplicate]

This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 6 years ago.
I have some object like this :
object(stdClass)#188 (1) { ["0"]=> string(1) "1" }
How could I print that object value in php ?
Thank you.
Try this, Best way for single value
echo $objects->{"0"};
and for multiple values
foreach($objects as $val )
{
echo $val;
}
Very short answer
var_dump($obj->{'0'});
You have access to the internal content of the Object via: ->{"0"}. Assuming you want to use the variable or echo it out for some reasons, you can simply do:
<?php echo $object->{"0"}; ?>

Array to string conversion in......array [duplicate]

This question already has answers here:
Display array values in PHP
(9 answers)
Closed 7 months ago.
I want to get the average of a column in a table,
table: buy
column: c1
when I called the database
with this:
$query="Select AVG(c1) as average FROM buy";
$result_array=mysql_query($query);
$line = mysql_fetch_array($result_array);
and when I called with php like this
<?php echo $line; ?>
it came up error with this message
Array to string conversion in .......... on line 50
Array
what did I do wrong? I think because I treated arrays as a string. but how can i fix this?
Please take a look that $line returns an array. So, you can't echo an array. One thing you can do is
echo "<pre>";
print_r($line);
Check what the array looks like.
Is it a single row that's been returned? In that case you can write
echo $line['average'];
If it's more than one row:
while ($line = mysql_fetch_array($result_array)) {
echo $line['average'];
}
Hope this helps.
Peace! xD

How to produce a line space in arrays [duplicate]

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"
}

How to echo an array [duplicate]

This question already has answers here:
How can I echo or print an array in PHP?
(14 answers)
Closed 2 years ago.
It's not like I've never done it before, but for some reason it won't work this time... I'm just returnin an array from a function
//Call Function to create the result array
$specs = giveData();
and I'm trying to output the data with echo this way:
<b>lenght:</b><?php echo $specs[0]['lenght']; ?>
I already tried var_dump and it shows me the data in the array, also with print_r works.
EDIT: I updated the code the way it works for me.
Print all values of an array
<?php
echo '<pre>';
print_r($specs);
// OR var_dump to get variable type (string / int / etc)
var_dump($specs);
echo '</pre>';
?>
The echo of the pre tags are for formatting reasons in HTML since the pre tag will show linebreaks (\n) as a visible new line inside of HTML.
As for echoing a single value from an array, all you have to do is refernce the key like you were doing.
echo $specs['length'];
You can make sure the key exists by using the function isset.
if(isset($specs['length'])) {
echo $specs['length'];
}else{
echo 'Error, Length not found';
}
The functions used in this answer can be found on the PHP.net website var_dump(), print_r() and isset()
not sure if you want the number of elements of the array :
echo count($specs);
or iterate over your array :
foreach($specs as $key => $value){
echo "$key : $value<br/>";
}

php json encode outputting invalid json from mysql results [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
encode json using php?
$hello_world = $this->session->all_userdata();
foreach($hello_world as $key=>$product_id)
{
$query['products'] = $this->Global_products->globalFindProductsViewed($product_id);
foreach($query['products'] as $product)
{
$ryan[] = $product->name;
}
}
foreach($ryan as $r)
{
echo json_encode(array($r));
}
The output then looks like this:
["Alpine 50W x 4 AppleĀ® iPodĀ®-Ready In-Dash CD Deck"]
I know I cant access this with JavaScript.
Can someone suggest how I can make this work?
JSON encoding every array element separately doesn't make sense.
Remove the foreach, and just do a
echo json_encode($ryan);

Categories