How to Use JSON_Encode

how to use json_encode

json_encode is a convenience method to convert an array into JSON format. To have the output you provided, you will need an array of arrays. Each sub-array has keys "name" and "data", where "name" is the Item column, and "data" is another array containing values from 2011 and 2012.

$results = mysql_query("...");
$arr = array();

while ($row = mysql_fetch_assoc($results))
{
$name = $row['Item'];
$data = array($row['2011'], $row['2012']);

$arr[] = array('name' => $name, 'data' => $data);
}

echo json_encode($arr);

How to use json_encode to format PHP to javascript for Chart?

You're double-encoding some of the data. Look at this:

json_encode(array_values($val_ca_1), JSON_NUMERIC_CHECK)

That will pre-encode that part of the data as a string inside $dataSets. So it's already JSON. When you come to convert the whole of $dataSets to JSON, it treats it as the simple string it's already been converted to, rather than an array.

The solution is simple - don't encode different parts of the data separartely. Keep it all in PHP variables and then turn all of it into a JSON string right at the last moment, when you actually need to.

In practical terms, in your code, just change

'data' => json_encode(array_values($val_ca_1), JSON_NUMERIC_CHECK)

to

'data' => array_values($val_ca_1)

How to use json_encode with mix of html and php outside PHP tags

With some research I've managed to figure this out.
ob_get_clean() and ob_start() is what I needed.

<?php ob_start(); ?>
<div>First huge mix of php and html goes <?php echo "here"; ?></div>
<?php $one= ob_get_clean();
ob_start(); ?>
<div>Second huge mix of php and html goes <?php echo "here"; ?></div>
<?php $two= ob_get_clean();
echo json_encode(array(
'one' => $one,
'two' => $two
)); ?>

and in ajax I used

success:function(data) 
{
$('#dividone').html(data.one);
$('#dividtwo').html(data.two);
}

PHP Array to JSON Array using json_encode();

If the array keys in your PHP array are not consecutive numbers, json_encode() must make the other construct an object since JavaScript arrays are always consecutively numerically indexed.

Use array_values() on the outer structure in PHP to discard the original array keys and replace them with zero-based consecutive numbering:

Example:

// Non-consecutive 3number keys are OK for PHP
// but not for a JavaScript array
$array = array(
2 => array("Afghanistan", 32, 13),
4 => array("Albania", 32, 12)
);

// array_values() removes the original keys and replaces
// with plain consecutive numbers
$out = array_values($array);
json_encode($out);
// [["Afghanistan", 32, 13], ["Albania", 32, 12]]

how to use array_push for json_encode

no need to assign it to $data[]. You are already pushing the values to the array $data

Just simply use

 array_push($data, array('id' => $row['id']));

instead of

 $data[] = array_push($data, array('id' => $row['id']));

json_encode() escaping forward slashes

is there a way to disable it?

Yes, you only need to use the JSON_UNESCAPED_SLASHES flag (PHP 5.4+).

!important read before: https://stackoverflow.com/a/10210367/367456 (know what you're dealing with - know your enemy: DO NOT USE in web/html context - CLI, unless CGI, might be fine thought, if they think they need it in JSON HTTP context for readability purposes, they have a different problem)

json_encode($str, JSON_UNESCAPED_SLASHES);

If you don't have PHP 5.4 at hand (you certainly already asserted the warning above), pick one of the many existing functions and modify them to your needs, e.g. http://snippets.dzone.com/posts/show/7487 (archived copy).

Example Demo

<?php
/*
* Escaping the reverse-solidus character ("/", slash) is optional in JSON.
*
* This can be controlled with the JSON_UNESCAPED_SLASHES flag constant in PHP.
*
* @link http://stackoverflow.com/a/10210433/367456
*/

$url = 'http://www.example.com/';

echo json_encode($url), "\n";

echo json_encode($url, JSON_UNESCAPED_SLASHES), "\n";

Example Output:

"http:\/\/www.example.com\/"
"http://www.example.com/"

Using a Json_encode to then be passed to a data attribute

My php that is producing this seems to add ='

That isn't what the PHP is producing.

You are looking at it in a DOM inspector. That shows the results of parsing the HTML to a DOM and then serialising it back to HTML for display.

If you want to look at the output of the PHP then you need to look at View Source and not Inspect Element.


JSON uses " to delimit strings.

An HTML attribute value that is delimited by " characters will be terminated by " characters.

To represent a " as data in an HTML attribute value; use ".

Run the output of json_encode through htmlspecialchars before outputting it into the HTML.

data-category="<?php echo htmlspecialchars(json_encode($arr)); ?>"

json_encode function: special characters

Your input has to be encoded as UTF-8 or ISO-8859-1.

http://www.php.net/manual/en/function.json-encode.php

Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.


Since 5.5.0 The return value on failure was changed from null string to FALSE.

Using json_encode on objects in PHP (regardless of scope)

In RedBeanPHP 2.0 there is a mass-export function which turns an entire collection of beans into arrays. This works with the JSON encoder..

json_encode( R::exportAll( $beans ) );


Related Topics



Leave a reply



Submit