I was wonder whether it is possible to convert a json Array to a single json object in php?
I mean below json Array:
[{'x':'y','k':'l'}, {'q':'w', 'r':'t'}]
Can be converted to:
{'0':{'x':'y','k':'l'}, '1':{'q':'w', 'r':'t'}}
Use json_encode with the JSON_FORCE_OBJECT parameter
$json = "[{'x':'y','k':'l'}, {'q':'w', 'r':'t'}]";
$array = json_decode($json, true); // convert our JSON to a PHP array
$result = json_encode($array, JSON_FORCE_OBJECT); // convert back to JSON as an object
echo $result; // {"0":{"x":"y","k":"l"},"1":{"q":"w","r":"t"}}
JSON_FORCE_OBJECT
Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty. Available since PHP 5.3.0.
Please try below code.
$array[] = array('x' => 'y','k' => '1');
$array[] = array('q' => 'w','r' => 't');
echo json_encode($array,JSON_FORCE_OBJECT);
Related
I have an unserialized array. Now i want to cut the first part of it the _PHP_Incomplete_Class_Name:"Payslip_Label". For example, below is the array that i want to cut
[{"__PHP_Incomplete_Class_Name":"Payslip_Label","label":"Position","value":"HR
Assistant","variable":"position"},
{"__PHP_Incomplete_Class_Name":"Payslip_Label","label":"Monthly
Rate","value":"10000","variable":"monthly_rate"}]
I want output like this
[{"label":"Position","value":"HR
Assistant","variable":"position"},
{"label":"Monthly
Rate","value":"10000","variable":"monthly_rate"}]
simple, just unset the unwanted keys
<?php
$json ='[{"__PHP_Incomplete_Class_Name":"Payslip_Label","label":"Position","value":"HR Assistant","variable":"position"},{"__PHP_Incomplete_Class_Name":"Payslip_Label","label":"Monthly Rate","value":"10000","variable":"monthly_rate"}]';
$data = json_decode($json,true);
foreach($data as $d => $i)
{
unset($data[$d]['__PHP_Incomplete_Class_Name']);
}
$json = json_encode($data);
print_r($json);
This question already has answers here:
PHP: Give a name to an array of JSON objects?
(5 answers)
Closed 3 years ago.
I need to convert this string
$json_string = [{"insert":"Test11"},{"insert":"","attributes":{"heading":3}}];
Into this json array
{
"ops":
[{"insert":"Test11"},{"insert":"","attributes":{"heading":3}}]
}
I converted the original string into array like this
$array = json_decode($json_string);
Now how to create a json object named "ops" that contains this array to be parsed using delta parser https://github.com/nadar/quill-delta-parser?
You could decode it, wrap it in an array with a key "ops" and encode it again
$json_string = json_encode(["ops" => json_decode($json_string, true)]);
echo $json_string;
Output
{"ops":[{"insert":"Test11"},{"insert":"","attributes":{"heading":3}}]}
You should make named index array and encode again like this:
$json_string =' [{"insert":"Test11"},{"insert":"","attributes":{"heading":3}}]';
$array = json_decode($json_string);
$array['ops'] = $array;
echo json_encode($array);
This works too using type casting as shorthand:
(object)['ops'=>'[{"insert":"Test11"},{"insert":"","attributes":{"heading":3}}]']
/* Output
object(stdClass)#1 (1) {
["ops"]=>
string(50) "[{insert:Test11},{insert:,attributes:{heading:3}}]"
} */
It's efficient, easy to keep in mind, easy to read and this can save using intermediary variables.
$var = {"4":true,"6":true,"8":true}
In The above string I want to get numbers into array.
Need: $var2 = [[0]=>4, [1]=>6, [2]=>8];
All response will be appreciated.
You should use json_decode and array_keys to accomplish it:
array_keys(json_decode($var, true));
First you decode the string using json_decode, the second argument means that the function should return an associative array and non an array of objects. This willl help up get the array keys.
$decoded = json_decode($var, true);
You get the array keys with this loop and place them in $var
foreach($decoded as $key => $value){
$var2[] = $key;
}
As i comment, use array_keys, and json_decode.
I don't believe that this question has an answer, So i did't answer
it. But i did it later.
You have an json, so you need to use json_decode now you jave an array where your keys are the desired value. so use array_keys.
$var = '{"4":true,"6":true,"8":true}';
$arr = json_decode($var, true);
echo '<pre>';
print_r(array_keys($arr));
Result:
Array
(
[0] => 4
[1] => 6
[2] => 8
)
My String is
["Alchemy","Alchemy-w","Alchemy-b"]
How can I made it to like
Array (
[0] => Alchemy
[1] => Alchemy-w
[2] => Alchemy-b
}
Please help me, How I Can get this output in a Stranded manner else I have to cut it using sub-string or any other ordinary php functions.
the string is a valid json-string. so you could use json_decode:
$json = '["Alchemy","Alchemy-w","Alchemy-b"]';
var_dump(json_decode($json));
Your string is json. just do this:
<?php
$json = '["Alchemy","Alchemy-w","Alchemy-b"]';
$array = json_decode($json, true); //When TRUE, returned objects will be converted into associative arrays.
echo "<pre>"; //to get it displayed nicely
print_r($array);
?>
Your string is json format only, you can use following mathod.
$resp = '["Alchemy","Alchemy-w","Alchemy-b"]';
print_r(json_decode($resp));
I have a bunch of values and a PHP array and I need to convert it to a JSON value for posting via CURL to parse.com
The problem is that PHP arrays are converted to JSON objects (string as key and value, vs string as just value)
I end up with
{"showtime":{"Parkne":"1348109940"}}
Rather then
{"showtime":{Parkne:"1348109940"}}
And parse complains that this is a object not an array and therefore won't accept it.
As
{"showtime":{"Parkne":"1348109940"}}
is a JSON object (key = a string)
Is there anyway to do this using json_encode? Or some solution?
That's the JSON spec: Object keys MUST be quoted. While your first unquoted version is valid Javascript, so's the quoted version, and both will parse identically in any Javascript engine. But in JSON, keys MUST be quoted. http://json.org
Followup:
show how you're defining your array, unless your samples above ARE your array. it all comes down to how you define the PHP structure you're encoding.
// plain array with implicit numeric keying
php > $arr = array('hello', 'there');
php > echo json_encode($arr);
["hello","there"] <--- array
// array with string keys, aka 'object' in json/javascript
php > $arr2 = array('hello' => 'there');
php > echo json_encode($arr2);
{"hello":"there"} <-- object
// array with explicit numeric keying
php > $arr3 = array(0 => 'hello', 1 => 'there');
php > echo json_encode($arr3);
["hello","there"] <-- array
// array with mixed implicit/explicit numeric keying
php > $arr4 = array('hello', 1 => 'there');
php > echo json_encode($arr4);
["hello","there"] <-- array
// array with mixed numeric/string keying
php > $arr5 = array('hello' => 'there', 1 => 'foo');
php > echo json_encode($arr5);
{"hello":"there","1":"foo"} <--object
Blind shot... I have the impression that your PHP data structure is not the one you want to begin with. You probably have this:
$data = array(
'showtime' => array(
'Parkne' => '1348109940'
)
);
... and actually need this:
$data = array(
array(
'showtime' => array(
'Parkne' => '1348109940'
)
)
);
Feel free to edit the question and provide a sample of the expected output.
You can convert your array to JSON using json_encode
assuming your array is not empty you can do it like this
$array=();
$json = json_encode($array);
echo $json;
It sounds like you need to take your single object and wrap it in an array.
Try this:
// Generate this however you normally would
$vals = array('showtime' => array("Parkne" => "1348109940"));
$o = array(); // Wrap it up ...
$o[] = $vals; // ... in a regular array
$post_me = json_encode($o);