how to unserialize jquery serialized data in php? - php

I have sent the serialized data to PHP on form submission:
$('#new-store-pickup').on("click",function(){
var businessHoursManager = $("#businessHoursContainer3").businessHours();
$('#businesshourvalue').val(JSON.stringify(businessHoursManager.serialize()));
$('#new-product-form').submit();
return false;
});
The data format is below:
[{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":false,"timeFrom":null,"timeTill":null},{"isActive":false,"timeFrom":null,"timeTill":null}]
How do I unserialize the above data in PHP?

Simple use json_decode for decode json data in php
<?php
$json = '[{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":false,"timeFrom":null,"timeTill":null},{"isActive":false,"timeFrom":null,"timeTill":null}]' ;
echo "<pre>";
$converted = json_decode($json);
print_r($converted);
?>

Related

ajax json response containing html

Do I need to json_encode ajax response data before sending?
$html1 = '<span>some html</span>';
$html2 = '<span>some html</span>';
$res = array('html1'=>$html1, 'html2'=>$html2);
echo json_encode($res);
or
echo $res;
The X in AJAX stands for XML
If you return XHTML, you do not need to json_encode it
echo '<div>'.$html1.$html2.'</div>';
You can
dump it onto the page someElement.innerHTML = returnValue; or document.body.insertAdjacentHTML('afterend',returnValue);
Extract from a DOMFragment:
const domFragMent = document.createElement("div")
domFragment.innerHTML = returnValue;
const spans = domFragment.querySelectorAll("span");
use a domParser
Send a json_encoded array of values and parse them on the client into spans

PHP input to JSON file and JSON file to PHP output

Can anyone please shows me how to get the input of PHP to JSON file (.json) and read data from JSON file and display in PHP (Echo).
for example:
$myObj->name = "John";
$myObj->age= 20;
to result.json
{"name":"John","Age":20}
and retrieve from result.json and display data in PHP as
name=John
Age=20
To convert the object to json use this:
$json = json_encode($myObj);
See the json_encode docs.
To return it back to the format you want try this...
$obj = json_decode($json);
$name = $obj->name; // John
$age = $obj->age; // 20
See the json_decode docs.
To iterate over keys and values do something like this:
foreach($obj as $key=>$value)
{
echo $key . " = " . $value . "\n";
}
json_encode() is used for encoding PHP data into a JSON format and json_decode() is used to decode JSON into a PHP data
json_encode documentation:
http://php.net/manual/en/function.json-encode.php
json_decode documentation
http://php.net/manual/en/function.json-decode.php
if you are having an php array you can convert it into json.
$json_string = json_encode($array);
and write this into a json file.
$fp = fopen('results.json', 'w');
fwrite($fp, json_string);
fclose($fp);
now convert your json string which is in results.json to array.
$str = file_get_contents('./results.json');
$array = json_decode($str, true); // decode the JSON into an associative array

PHP Issue displaying (echo) JSON object items

I have a PHP file which is displaying some posted data:
$data = file_get_contents('php://input');
echo json_encode($data);
The above returns:
{"name":"mark","item":"car"}
Now I want to echo just the name so I tried:
echo $data[0].name;
But that's giving me Error: [Object].[Object]
How can I fix this?
You need to decode your JSON input first:
// $data is an input string
$data = file_get_contents('php://input');
// convert input string to PHP array
$data = json_decode(data, true);
// echo just the name
echo $data['name'];
// dump the whole parsed input
var_dump($data);

HOW TO PHP JSON DATA convert PHP Treeview

Hi Guys I have a JSON data I need to convert this data Treeview a json data url: http://torrent2dl.ml/json.php
recovered state = http://torrent2dl.ml/json.php?tree
I tried to do http://torrent2dl.ml/hedef.php
how to convert this data a php function or code ?
json_decode($jsonObject, true);
Use json_decode() :
<?php
$url = 'http://torrent2dl.ml/json.php';
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON);
var_dump($data);
?>
Source : https://stackoverflow.com/a/8344667/4652564

remove string in json that contains value via jquery or php

I have json string, which is produced by server.php
[{"attr":{"id":"node_7","rel":"default"},"data":"doc.html","state":""},
{"attr":{"id":"node_8","rel":"folder"},"data":"New node","state":"closed"},
{"attr":{"id":"node_9","rel":"folder"},"data":"New node","state":""}]
How do I remove a full string that contains the value rel=default
This is the code I have for server.php.
require_once("config.php");
$jstree = new json_tree();
echo $jstree->{$_REQUEST["operation"]}($_REQUEST);
die();
Using PHP:
// convert json string to array
$json = json_decode($json_string);
// filter out items
$json = array_filter($json, function($item)
{
return $item->attr->rel != "default";
});
// convert back to string
$json_string = json_encode($json);
You can use data.items.splice(X, Y); to remove an element ;)

Categories