I have managed to get data from database in PHP file.
From there(data.php),
$output = json_encode($result);
The result would be like this,
$output=[{"kitty":"Whitely"},{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}]
So how do I give name "kitten" an array of kitty objects in php format?
For example like
"kitten":[{"kitty":"Whitely"},{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}]
You have to wrap your result in another array on the 'kitten' key :
$output = json_encode(['kitten' => $result]);
Try this:
<?php
$kitty = array('kitten' => array());
$kitty['kitty'][] = array('kitty' => 'Tabby');
$kitty['kitty'][] = array('kitty' => 'Ruby');
$kitty['kitty'][] = array('kitty' => 'Silver');
var_dump($kitty);
var_dump(json_encode($kitty));
which results in: {"kitty":[{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}]}
Use nested encode and decode
$json = '[{"kitty":"Whitely"},{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}]';
echo json_encode(array('kitten' => json_decode($json)));
try to use this
$output['kitty'][] = json_encode($result);
$result =array('kitten'=> $output);
output
{
"kitten":[
{"kitty":"Whitely"},
{"kitty":"Tabby"},
{"kitty":"Ruby"},
{"kitty":"Silver"}
]
}
Related
I have this array:
$users = array();
// Finally, loop over the results
foreach( $select as $email => $ip )
{
$users[$email] = $ip;
}
Now i want to pass this array to ajax and print in another script:
$html="<center>Check users for spam</center>";
mcheck.php
echo $_POST['users'];
This code does not work. How can i do this?
As Sergiu Costas pointer out, the best way is to JSON encode it and then decode, but you can also try:
$html="<center>Check users for spam</center>";
If your $users array is:
$users = array( array('email' => 'a#email.com'), array('email' => 'b#email.com')));
then http_build_query('users' => $users) will generate:
users%5B0%5D%5Bemail%5D=a%40email.com&users%5B1%5D%5Bemail%5D=b%40email.com
which is the url-encoded version of:
users[0][email]=a#email.com&users[1][email]=b#email.com
which will be decoded back to array in mcheck.php
The Best way is to encode array into JSON format.
In PHP:
$users_json = json_encode($users);
// To decode in javascript
var obj = JSON.parse(json_str);
if u want to pass php array to js you can json_encode the array to do that.
$html="<center>Check users for spam</center>";
I'm attempting to use json_encode on a php array. I have to structure the returned JSON as:
[
{"text": "Title1"},
{"text": "URL"}
]
I've tried the following, but I keep getting 0 as a key.
$xml = simplexml_load_file($url);
$title1 = $xml->results->result->jobtitle;
$snippet1 = $xml->results->result->snippet;
$url1 = $xml->results->result->url;
$arrays = array('text'=>$title1);
echo json_encode($arrays);
What am I doing wrong with my encoded array? How do I have it so that it doesn't return as 0?
{"text":{"0":"CDL-A Dry Bulk Drivers Wanted - Regional - OH, WV, PA"}}
Please try this: You have no mistake in json_encode.
$title1 = $xml->results->result->jobtitle;
...
$arrays = array('text'=>$title1[0]);
They way you are setting your array is incorrect. What you want to do is.
$array = [
['text' => 'hello'],
['text' => 'hello again'],
];
$encoded = json_encode($array);
print_r($encoded);
which returns
[
{"text":"hello"},
{"text":"hello again"}
]
I have in a file 3 json responses. I need to encode them in one Json with 3 Objects inside which are the three responses which I have separated. Any help?-
echo json_encode(array('result_temperatura'=>$output_result_temperatura));
echo json_encode(array('result_presion'=>$output_result_presion));
echo json_encode(array('result_altitud'=>$output_result_altitud));
Build an array or object with the data:
$result = array('result_temperatura'=>$output_result_temperatura,
'result_presion'=>$output_result_presion,
'result_altitud'=>$output_result_altitud);
echo json_encode($result);
Or if you actually want a multidimensional array:
$result[] = array('result_temperatura'=>$output_result_temperatura);
$result[] = array('result_presion'=>$output_result_presion);
$result[] = array('result_altitud'=>$output_result_altitud);
echo json_encode($result);
Combine the array elements first,
$arr = array(
'result_temperatura' => $output_result_temperatura,
'result_presion' => $output_result_presion,
'result_altitud' => $output_result_altitud
);
Then encode,
echo json_encode($arr);
Hope this helps.
Assuming i have a JSON array like this example below :
[{"type":"food","alias":"meal"}]
And i want to assign this whole array to a particular key called "Dish" for example.
How can i archive this ?
Expected output should be something like :
"Dish":[{"type":"food","alias":"meal"}]
I know how to create new key value pairs but never thought of assigning a key until now.
$json = '[{"type":"food","alias":"meal"}]';
$data = json_decode($json, true);
$data = array('Dish' => $data);
echo json_encode($data);
You could do like this..
<?php
$json = '[{"type":"food","alias":"meal"}]';
$arr = array('dish'=>json_decode($json,true));
echo json_encode($arr);
Demo
echo json_encode(array('Dish' => json_decode($json, true)));
//{"Dish":[{"type":"food","alias":"meal"}]}
I have a folowing JSON file:
{"ver":2,"sb":[some array],"ld":[some array] ,"hd":[some array]}
I try to ger property names with next code:
$path='./datafiles/jsonTest.json';
$data = json_decode(file_get_contents($path));
$properties=get_object_vars($data);
foreach($properties as $propName){
echo $propName.'<br>';
}
but as result I get:
2
Array
Array
Array
when I need:
'ver'
'sb'
'ld'
'hd'
Can someone help me? Thanks!
If you don't need the resulting output as an object, you coud use the array version of json_decode
$data = json_decode(file_get_contents($path), true);
$properties = array_keys($data);
you can also try using json_decode to give you an associative array
$path='./datafiles/jsonTest.json';
$data = json_decode(file_get_contents($path),true);
foreach($data as $name => $value){
echo $name.'<br>';
}
Have you tried the key?
foreach($properties as $key => $propName){
echo $key.'<br>';
}
You can either use reflection to get the properties names (http://php.net/manual/en/reflectionclass.getproperties.php) - which would be the elegant way - or you decide not to decode the Json data and extract the names and values with manual string operations.