How to JSON_Encode PHP Array But the Keys Without Quotes

How to json_encode php array but the keys without quotes

First, you have to generate your array in php so the data's value are integers, not strings:

I emulated your array from your json_encode(), I guess it looks like this (or it should):

$array =  array(
array("label" => "Crear Usuario", "data" => 2),
array("label" => "Impresoras", "data" => 1),
array("label" => "Problema Correo", "data" => 1),
array("label" => "Requisicion Equipo", "data" => 1),
array("label" => "Sitio Web", "data" => 1)
);

$data = json_encode($array);
  • Notice that the 2 and 1's are unquoted, so this way they are integers, this is important.

Then you are missin in Javascript the JSON.parse() to actually make that output into a json object:

<script>
var data = '<?php echo $data; ?>';
var json = JSON.parse(data);
console.log(json);
console.log(json[0]);
</script>
  • Notice that var data = ... is SINGLE QUOTED, so you catch the echo from php as a String

The console.log()'s output this for me:

[Object, Object, Object, Object, Object] // First console.log(): one object with the 5 Objects. 
Object {label: "Crear Usuario", data: 2} // secons console log (json[0]) with the first object

Looks like what you need, am I right?

json_encode remove quotes from keys?

When I test this portion of code :

echo json_encode(array( 'foo' => 'bar'));
die;

I get :

{"foo":"bar"}

Which is valid JSON.

(Note these are double-quotes, and not simple-quotes as you posted)


The ouput you are asking for :

{ foo : 'bar' }

is valid Javascript, but is not valid JSON -- so json_encode will not return that.

See json.org for the specification of the JSON format -- which is a subset of Javascript, and not Javascript itself.


Instead of "stripping the quotes out myself with some ugly regex", you should adapt your code, so it accepts valid JSON : this is way better, in my opinion.

Remove double quotes from json_encode array

If Google wants it in that particular format, simply feed him :)

I supposed the json_encoded result stored in $json.

So, you can do this:

$json = str_replace('"','', (string) $json);

remove quote on echo json_encode();

Try trim in php

<?php 
$name = "Aman";
$str = json_encode($name);
echo trim($str, '"'); // output : Aman
?>

remove quotes from json_encode keys

You are experiencing this issue because you are not using json correctly.

You can still just echo the json_encode() into the JS variable but get rid of the [] brackets and then use .parseJSON() to get what you need:

var availableTags = $.parseJSON('<?php
// Database Connection
error_reporting(-1);
ini_set('display_errors', 'On');

$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

$q="SELECT name as value,term_id as id FROM 7terms WHERE term_id not in
(select parent from 7term_taxonomy) and term_id in (select term_id from
7term_taxonomy where taxonomy='cat')";

$r = mysqli_query($con, $q);

$city_state = array();
while($row = mysqli_fetch_assoc($r))
{
$rows[]=$row;
}
echo json_encode($rows,true);
?>');

This solution should get you going but I would recommend opting for an AJAX based solution where your query is in a PHP file and call upon it with AJAX because $.ajax() can auto-convert a JSON string into the properly formatted array/object expected as long as you set dataType: 'json'

PHP - JSON_ENCODE - Remove quotes from value

Well, so easy.

The situation: I need to bring number to put together with highstock, and was not bringing anything.

After I just did a new array with int numbers, this way is coming without double quotes.

I don't know why people give negative without ask why the question.

Closed.

Remove double quote in json_encode()

If you add an underscore to your regex at the end it will do it.

$array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:',$array_final);

I assume that's what that preg_replace is for anyway.

Create array from json_encode script without double quotes

Assuming you're trying to turn stringified JSON into an array, and turn the strings inside the array into numbers. You can use some combination of .map() and .filter() to achieve this.

http://jsbin.com/yojibeguna/1/edit?js,console

var artistJSON = JSON.parse('["31","41","56","38","","27"]')
.map(function (e) { return parseInt(e); })
.filter(function (e) { return isFinite(e); });

console.log(artistJSON, typeof artistJSON[0]);


Related Topics



Leave a reply



Submit