PHP JSON format object - php

PHP Noob here...
if i have JSON format: {"0":"0x1001C","1":"0"}
but what i really want is is: {"0x1001C","0"}
how do i attain this?
esentially, i have an array which contains keys and values.
i'm removing the keys (as this is a requrement) and the final array must be in object format (in other words, JSON has to return something with {} rather than [] )
Any ideas?
Thanks in advance

echo json_encode(array('0x1001c', '0'), JSON_FORCE_OBJECT);
^^^^^^^^^^^^^^^^^
as per the docs: http://php.net/json_encode

Related

PHP display array in array in array

I want know how to access to array in array in array in php.
The result with $user is this:
'pages':{'x','y','z','access':{'a':3,'b':6,'c':8,'contact':2}}
How I can access to contact, please?
First of all this is JSON (probably), so before we can access it with php we need to decode it using json_decode which will give you a php object. (i made it valid JSON)
$jsonString = '{"pages":{"x": 0,"y": 0,"z": 0,"access":{"a":3,"b":6,"c":8,"contact":2}}}';
$phpObject = json_decode($jsonString);
var_dump($phpObject->pages->access->contact);
// prints int(2)
Use json_decode to convert this to array in PHP (if it is JSON)
It seems your string missing {} around.
{
"id":1,"active":1,"canAccess":{"entities":{"1":{"name":"blablabla","services":{"‌​45":{"name":"xxx"}}}}},"blabla":null,"pages":{"acces":{"notifications":1,"contact‌​":2}}
}
then it is a valid JSON string.
var_dump(json_decode($string,true));

php json_encode() output changes based on the array indices

I was trying to get some values from MySQL database and when converting into JSON array using json_encode() I got a JSON object , after a while I found out that the indices was the root cause of the problem
here's the example I mentioned
<?php
$array = array(0=>"zero",2=>"two");
$another_array=array(0=>"zero",1=>"one");
print_r(json_encode($array)); // output: {"0":"zero","2":"two"}
print_r(json_encode($another_array)); //output: ["zero","one"]
?>
so what's the reason for that ?
Because array(0=>"zero",1=>"one") is the same as array("zero", "one") and the JSON encoder assumes the latter is a list (since that's how PHP does lists) and has no way to tell the former from the latter.
If you want to force json_encode to treat all arrays as objects, pass JSON_FORCE_OBJECT as the second argument:
json_encode(array(0=>"zero",1=>"one"), JSON_FORCE_OBJECT)
// {"0":"zero","1":"one"}
If you always want a json list instead, get rid of the keys (e.g. using array_values()) before encoding.

interpreting JSON with PHP and putting it in array

I have only worked with JSON once before, but I don't recall how to use it with PHP.
If I have a script that returns JSON like this:
{
"bunisess":[
"business",
"bonuses",
"burnooses",
"boniness",
"burnoose's"
]
}
how can I take that and make each value a value in a PHP array. The keys just being numbers from 0 onwards?
Use json_decode, but pass true as the second parameter to get an associative array back:
$json='{"bunisess":["business","bonuses","burnooses","boniness","burnoose\'s"]}';
$data=json_decode($json, true);
Now, you can use array_values to get a numerically indexed array as you required:
$data=array_values($data);
A simple google search could have lead you to this page : http://php.net/manual/fr/function.json-decode.php
The subarray will be respecting exatcly what you're asking for.
Assuming that $data is the complete JSON string
$stdObject = json_decode($data, true);
print_r($array);
You should get an array with one key bunisess and value another numeric array with the other values.

PHP to JSON encoding

I am trying to write this using php and json_encode
{"patient": {"demographics": {}}
}
This is the Array I am using with json encode.
Array("patient" => Array("demographics" => Array()))
When I echo the output, this is what I get:
{"patient":{"demographics":[]}}
I really think this is a stupid mistake ob my part. All help is appreciated.
try
json_encode($your_array, JSON_FORCE_OBJECT)
as per the docs: http://php.net/json_encode
By default, php arrays stay arrays ([]) when json'd, unless there's a single non-numeric key, in which case it'll be an object
You can also try this:
json_encode(Array("patient" => Array("demographics" => new stdClass)));
This way you can have empty arrays and empty objects in the same JSON, which would not be possible using the other answer.

Help editing JSON to make an array rather than a 'dictionary'

I currently have json using json_encode from a mysql query which looks like this:
{"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}, {"post_2":{"caption":"...","id":"...","accountID":"..","date":"07\/07\/2011 1:45:12 AM","title":"...","authorInfo":{"Email Address":"..."}}}
How can I have the json being an array of posts ('post_2', 'post_1') rather than it being a dictionary? The JSON will be decoded on an iPhone using SBJSON and the JSON will have to be made into an array in the backend.
Thanks in advanced.
Provide a non-associative array to json_encode(). The easiest way is usually to simply call array_values() on the (associative) array, and encode the result.
Take a look at PHP's json_decode function, specifically the 2nd parameter if you want an array.

Categories