I generate an unknown number of arrays like this:
for ($t=0;$t<2;$t++) {
for ($xx=0;$xx<$totf[$t];$xx++) {
$otdata[$t][$xx] = array("".$otname[$t][$xx]."" =>
['games'=> $otg[$t][$xx],
'mint'=> $otqmint[$t][$xx],
'pct'=>$otpct[$t][$xx]]);
}
}
I need this arrays to be merged before encoding the result to a .jsonfile.
I do it like this:
$newot= array_merge_recursive($otfdata);
$myJsondata = json_encode($newot);
file_put_contents('json/myJson.json', $myJsondata);
The myJson.jsonfile that results is something like this:
[[{"John":{"games":"1","mint":"3.65","pct":"0"}},
{"Mary":{"games":"1","mint":"0.625","pct":"12"}},
...
{"Whatever":{"games":"1","mint":"0.325","pct":"4"}}]]
And I don't know how to manage this file. I mean, I would like to do something like this:
1) In php, I want to reach the "mint" data point of "Mary" => "0.625".
$myfile=file_get_contents("json/myJson.json");
$mydata = json_decode($myfile,true);
$myResult=$mydata["Mary"]["mint"];
Obviously, this does not work.
2) In jQuery, I want basically the same: to reach the "mint" data point of "Mary".
var gdatafile = "json/myJson.json";
$.getJSON(gdatafile, function (gjson) {
var myResult=gjson["Mary"].mint;
$('#resultContainer').text(myResult);
});
My problems:
A) I think the first problem I have is during the merging of the different arrays. The way I do it generates [[ at the beginning and at the end, and I think this is incorrect.
B) Apart from that, is it correct the way I do it in jQuery?
What I need:
1) To merge and encode the arrays in a way it looks like this:
{"John":{"games":"1","mint":"3.65","pct":"0"},
"Mary":{"games":"1","mint":"0.625","pct":"12"},
...
"Whatever":{"games":"1","mint":"0.325","pct":"4"}}
I think this would solve problem A.
2) To confirm wether the way I do it in jQuery is correct or not.
Thanks
nested arraysi.e, array of arrays php.net/manual/en/function.array-merge-recursive.php check the example