Generate json string from multidimensional array data
The simplest way would probably be to start with an associative array of the pairs you want:
$data = array("myfirstvalue" => "myfirsttext", "mysecondvalue" => "mysecondtext");
then use a foreach and some string concatenation:
$jsontext = "[";
foreach($data as $key => $value) {
$jsontext .= "{oV: '".addslashes($key)."', oT: '".addslashes($value)."'},";
}
$jsontext = substr_replace($jsontext, '', -1); // to get rid of extra comma
$jsontext .= "]";
Or if you have a recent version of PHP, you can use the json encoding functions built in - just be careful what data you pass them to make it match the expected format.
Multidimensional Array to JSON (and back) in Java
You can create a Map of AreaHash
objects then convert it to JSON string.
Map<String, AreaHash> data = new HashMap<>();
data.put("1", new AreaHash(...));
data.put("2", new AreaHash(...));
data.put("3", new AreaHash(...));
String jsonString = new Gson().toJson(data);
System.out.println(jsonString);
Output (Pretty printed)
{
"1": {
"Name": "Test",
"Owner": "gold",
"X": "10",
"Y": "10",
"Z": "0"
},
"2": {
"Name": "Test",
"Owner": "silver",
"X": "10",
"Y": "10",
"Z": "-10"
},
"3": {
"Name": "Test",
"Owner": "test",
"X": "10",
"Y": "10",
"Z": "-10"
}
}
Also, take note that your desired output is not valid JSON.
[
"Name": true,
"Owner": "gold",
"X": "10",
"Y": "15",
"Z": "-1000"
] // this will not parse
How to convert PHP Multidimensional Array to Json so it works as Json POST request?
To make json_encode
generate an array of json objects, the key bar2
needs to contain an array of arrays, i.e.
$myArray = array(
"bar2" => array(
array(
"hello" => "world"
),
),
);
Updating your original array to the following format will generate a json object with bar2
returning an array of objects.
$myArray = array(
"bar1" => "sample",
"bar2" => array(
array(
"bar1" => "sample",
"bar2" => "sample",
"bar3" => "111111111",
"bar4" => "sample",
"bar5" => [
"bar6" => "varr",
"bar7" => array(
-70.111111111111111,
-33.111111111111111
)
]
)
),
"bar8" => 11111
);
The following array will generate the following json.
{
"bar1": "sample",
"bar2": [
{
"bar1": "sample",
"bar2": "sample",
"bar3": "111111111",
"bar4": "sample",
"bar5": {
"bar6": "varr",
"bar7": [
-70.11111111111111,
-33.111111111111114
]
}
}
],
"bar8": 11111
}
Demo source.
Create multidimensional arrays on json string
This function take the daten['data'] array and create a list entry with 3 possitions array in first position and prodDatum value in second
function createHochOfenChart(daten){
var barValues = daten["data"].map(function (e){
var valus = new Array();
valus.push([e.reSoll, e.reIst, e.reVorrat])
valus.push(e.prodDatum);
return valus
})
return barValues
}
Is what you wanted?
Creating a JSON object from multidimensional array stored as string
It's pretty jerry-rigged, but you could do this:
$string = json_encode( $database_value ); // same as you're using now
$string = str_replace( '"', '', $string ); // get rid of the quotes
// wrap them back around 'coordinates'
$string = str_replace( 'coordinates', '"coordinates"', $string );
How to create a JSON Array from a two-dimensional array in Ruby
Your desired result is still a Ruby hash. To transform it call map
on the array and create a hash for each entry:
a = [
["01fe237f804a5eff182dcded115c37d3", 0.0140845],
["026e5f1f7f026bf3c763523206aa44bf", 0.03448275],
["04a1c3c79773bd1ecc0372a991adc815", 0.04617604]
]
a.map{ |e| {address: e[0], value: e[1]} }
This returns the desired result.
If you want to create a JSON string, require json
and do the following:
require 'json'
a = [
["01fe237f804a5eff182dcded115c37d3", 0.0140845],
["026e5f1f7f026bf3c763523206aa44bf", 0.03448275],
["04a1c3c79773bd1ecc0372a991adc815", 0.04617604]
]
a.map{|e| {address: e[0], value: e[1]} }.to_json
This will encode the result to the following string:
"[{\"address\":\"01fe237f804a5eff182dcded115c37d3\",\"value\":0.0140845},{\"address\":\"026e5f1f7f026bf3c763523206aa44bf\",\"value\":0.03448275},{\"address\":\"04a1c3c79773bd1ecc0372a991adc815\",\"value\":0.04617604}]"
Related Topics
PHP 5.4 Call-time pass-by-reference - Easy Fix Available
Bulk Insertion in Laravel Using Eloquent Orm
Getting Checkbox Values on Submit
Changing Upload_Max_Filesize on PHP
MySQLi::MySQLi(): (Hy000/2002): Can't Connect to Local MySQL Server Through Socket 'MySQL' (2)
Convert Command Line Curl to PHP Curl
How to Convert a Number to a Word in PHP
How to Loop Through Two Arrays At Once
Change Cart Item Prices in Woocommerce 3
Does PHP Allow Named Parameters So That Optional Arguments Can Be Omitted from Function Calls
Problem With PHP Mail 'From' Header
Calling Non-Static Method With Double-Colon(::)
Get Content Between Two Strings PHP
Url Rewriting: Css, Js, and Images Not Loading
Remove Index.PHP from Url - Codeigniter 2