I'm having some problems generating graphs with drilldown in Highcharts.
I'm using Highcharts to render a pie with drilldown series.
I need to transform this output json
Drilldownseries = [
{
"name": "LAZIO",
"data": [["ROMA", 28]],
"id": "LAZIO"
},
{
"name": "LAZIO",
"data": [["FROSINONE", 218]],
"id": "LAZIO"
},
{
"name": "LAZIO",
"data": [["LATINA", 212]],
"id": "LAZIO"
},
{
"name": "TOSCANA",
"data": [["FIRENZE", 2]],
"id": "TOSCANA"
},
{
"name": "TOSCANA",
"data": [["LIVORNO", 5]],
"id": "TOSCANA"
},
{
"name": "TOSCANA",
"data": [["PISA", 9]],
"id": "TOSCANA"
}
];
to
Drilldownseries = [
{
"name": "LAZIO",
"data": [["ROMA", 28], ["FROSINONE", 218], ["LATINA", 212]],
"id": "LAZIO"
},
{
"name": "TOSCANA",
"data": [["FIRENZE", 2], ["LIVORNO", 5], ["PISA", 9]],
"id": "TOSCANA"
}
];
This is the part of query that populates the array:
...
if($res3)
{
$i = 0;
while ($row = mysqli_fetch_array($res3, MYSQLI_ASSOC))
{
$row3[$i]["name"] = $row["name"];
$row3[$i]["data"] = [[$row["subname"],$row["data"]]];
$row3[$i]["id"] = $row["id"];
$i++;
};
$row3 = json_encode($row3,JSON_NUMERIC_CHECK);
};
I'd prefer to extract the array with php well formed, but should be the same tranform the json.
PHP 7.2
Highcharts 6.1.1
for benefit of all, this is my solution. Code refined should be appreciated :)
First removed square brackets in code below
$row3[$i]["data"] = [$row["subname"],$row["data"]];
then create the new array so
$repl = array();
$i = 0;
foreach ($row3 as $value) {
if (!isset($repl[$i]['id'])) {
$repl[$i]['id'] = $value['id'];
$repl[$i]['name'] = $value['name'];
$repl[$i]['data'] = [$value['data']];
} elseif (($repl[$i]['id']) <>$value['id']) {
$i++;
$repl[$i]['id'] = $value['id'];
$repl[$i]['name'] = $value['name'];
$repl[$i]['data'] = [$value['data']];
} else {
array_push($repl[$i]['data'], $value['data']);
}
}
$resultx = json_encode($repl,JSON_NUMERIC_CHECK);
thanks
Related
I have a code like this:
$res = array ();
$i2 = 0;
for ($i = 0; $i <10; $i++) {
if ($table ['users'] [$i] ['value'] == null) {
break;
}
$res ['users'] [$i2] ['value'] = $table ['users'] [$i] ['value'];
$res ['users'] [$i2] ['user_id'] = $table ['users'] [$i] ['user_id'];
$res ['users'] [$i2] ['name'] = $table ['users'] [$i] ['name'];
$i2 ++;
}
$mesto;
for ($i3 = 0; $i3 <count ($table ['users']); $i3 ++) {
if ($user_id == $table ['users'] [$ i3] ['user_id']) {
$mesto = $ i3 + 1;
break;
}
}
$res ['currentTop'] = $mesto;
$json = json_encode ($res, JSON_UNESCAPED_UNICODE);
echo $json;
$table * - This is decoded json, table has an array users, in which objects, each with three fields * value, user_id, name *
JSON looks like this:
{
"users": [{
"value": "994954359439",
"user_id": 2,
"name": "Anton Piskorskyi"
}, {
"value": 99999,
"user_id": 99999,
"name": "Anton Piskorskyi"
}, {
"value": 99998,
"user_id": 99998,
"name": "Anton Piskorskyi"
}, {
"value": 99997,
"user_id": 99997,
"name": "Anton Piskorskyi"
}, {
"value": 99996,
"user_id": 99996,
"name": "Anton Piskorskyi"
}, {
"value": 99995,
"user_id": 99995,
"name": "Anton Piskorskyi"
}, {
"value": 99994,
"user_id": 99994,
"name": "Anton Piskorskyi"
}, {
"value": 99993,
"user_id": 99993,
"name": "Anton Piskorskyi"
}, {
"value": 99992,
"user_id": 99992,
"name": "Anton Piskorskyi"
}, {
"value": 99991,
"user_id": 99991,
"name": "Anton Piskorskyi"
}]
}
Themselves such objects can be 100,000 or more. The above code takes the first 10 objects from the array and by * user_id * looks for the index of the object in which this * user_id * is located. In short, the code is used to display the top 10 players and the current position of the user. Those. this piece of code will be called quite a few times in a small amount of time. I have intel I3-71 on my computer ... and when I start spamming the opening of the page many times, the processor is loaded by ~ 50% (this is for 100,000 objects), if you put this code on the VDS, then with a large online VDS can not to export, hence the question arises: how can all this be optimized? namely this function:
$mesto;
for ($i3 = 0; $i3 <count ($table ['users']); $i3 ++) {
if ($user_id == $table ['users'] [$i3] ['user_id']) {
$mesto = $i3 + 1;
break;
}
}
P.S.*
Objects I call this in Json:
{
"value": "994954359439",
"user_id": 2,
"name": "Anton Piskorskyi"
}
I would like to know how to display the information below.
Link (JSON):
https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents
What I wish to have:
"teams": [
[
{
"provider": "psn",
"username": "Germania1992"
},
{
"provider": "psn",
"username": "killzoneprofi"
},
{
"provider": "psn",
"username": "ayozetf87"
},
{
"provider": "psn",
"username": "Seith911"
},
{
"provider": "psn",
"username": "domibreu92"
}
],
[
{
"provider": "psn",
"username": "Thejuankarboy"
},
{
"provider": "psn",
"username": "Gamermad101"
},
{
"provider": "psn",
"username": "Izdrap"
},
{
"provider": "psn",
"username": "Guerra_sv"
},
{
"provider": "psn",
"username": "TriX_FollOoW_YT"
}
]
],
I want to display the nicks of the different teams
Example: Team 1 = Germania1992, killzoneprofi, ayozetf87, Seith911,
domibreu92
Thanks
You will have to do a double foreach that for sure.
<?php
// Get the json of the team
$team = team("https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents");
// Display the teams info
foreach($team as $nb=>$data){
echo "Team $nb<br />";
foreach($data as $key=>$value){
echo "Provider: ".$value->provider."<br />";
echo "Username: ".$value->username."<br />";
}
echo "<hr>";
}
// Returns the team array of the json
function team($jsonURL){
$content=file_get_contents($jsonURL);
$data=json_decode($content);
return $data->data->teams;
}
?>
You could increment the team number by 1 to avoid the first being 0
The above will display the below screenshot (You can format the output as you please)
$result = [];
$counter = 0;
dump($array = json_decode(file_get_contents('https://my.callofduty.com/api/papi-client/ce/v1/title/bo4/platform/psn/match/11337378706913618925/matchMapEvents')));
dump($t = array_column((array)$array, 'teams'));
foreach ($t as $r) {
foreach ($r as $p) {
$counter++;
foreach ($p as $value){
$result["team$counter"][] = $value->username;
}
}
}
var_dump($result);
I have a code that outputs the json result below:
{
"Ghost in the Shell": {
"id": 1203,
"Pipeline": {
"id": 6144,
"name": "Pipeline",
"status": "New",
"item_id": 5962,
"TotalTasksOpen": 2
}
}
}
Now how can I format it in the my desired json format below:
Note* I have to remove some result.
{
"Ghost in the Shell":
"id": 6144,
"name": "Pipeline",
"status": "New",
"item_id": 5962,
"TotalTasksOpen": 2
}
Will appreciate if anyone can help me please.
I am not sure what is your purpose , but here is what you need
<?php
$json = '{
"Ghost in the Shell": {
"id": 1203,
"Pipeline": {
"id": 6144,
"name": "Pipeline",
"status": "New",
"item_id": 5962,
"TotalTasksOpen": 2
}
}
}';
$array = json_decode($json, true);
$newArray = array();
foreach($array as $key=>$value) {
$newArray[$key] = '';
foreach($value as $k=>$v) {
if(is_array($v)) {
$newArray = array_merge($newArray,$v);
}
}
}
$newJson = json_encode($newArray);
echo $newJson;
?>
I am trying to decode JSON data to PHP then output it to the site. If I have the following:
{
"name": "josh",
"type": "human"
{
I can do this (within PHP), to display or output my type:
$file = "path";
$json = json_decode($file);
echo $json["type"]; //human
So, if I have the following:
{
"name": "josh",
"type": "human",
"friends": [
{
"name": "ben",
"type": "robot"
},
{
"name": "tom",
"type": "alien"
}
],
"img": "img/path"
}
How can I output what type my friend ben is?
Use a loop like foreach and do something like the following:
//specify the name of the friend like this:
$name = "ben";
$friends = $json["friends"];
//loop through the array of friends;
foreach($friends as $friend) {
if ($friend["name"] == $name) echo $friend["type"];
}
To get the decoded data in array format you would supply true as the second argument to json_decode otherwise it will use the default which is object notation. You could easily create a function to shorten the process when you need to find a specific user
$data='{
"name": "josh",
"type": "human",
"friends": [
{
"name": "ben",
"type": "robot"
},
{
"name": "tom",
"type": "alien"
}
],
"img": "img/path"
}';
$json=json_decode($data);
$friends=$json->friends;
foreach( $friends as $friend ){
if( $friend->name=='ben' )echo $friend->type;
}
function finduser($obj,$name){
foreach( $obj as $friend ){
if( $friend->name==$name )return $friend->type;
}
}
echo 'Tom is a '.finduser($friends,'tom');
try this,
$friend_name = "ben";
$json=json_decode($data);
$friends=$json->friends;
foreach( $friends as $val){
if($friend_name == $val->name)
{
echo "name = ".$val->name;
echo "type = ".$val->type;
}
}
DEMO
I use php glob function for get images in sub directories and I dont know how many files exist in each directory
directory name is id and I want to categorize all images in directories in one array
$arr = [];
$dir = dirname(__FILE__)."/gulets";
$files = glob($dir."/*/*_gulet_o_*");
foreach ($files as $file) {
$fullname = str_replace($dir."/", "", $file);
$name = explode("/", $fullname);
$name1 = array('id'=>$name[0],'name'=>$name[1]);
$arr[] = $name1;
}
header('Content-Type: application/json');
echo json_encode($arr);
[
{
"id": "10",
"name": "asdsad_gulet_o_1.jpg"
},
{
"id": "10",
"name": "wqes_gulet_o_10.jpg"
},
{
"id": "10",
"name": "qwsdf_gulet_o_11.jpg"
},
{
"id": "10",
"name": "sdce_gulet_o_12.jpg"
},
{
"id": "11",
"name": "fsdsc_gulet_o_13.jpg"
},
{
"id": "11",
"name": "drfvc_gulet_o_14.jpg"
},
{
"id": "12",
"name": "dsyjhk_gulet_o_15.jpg"
},
.
.
and I need change it like this :
[
{
"id": "10",
"name1": "asdsad_gulet_o_1.jpg",
"name2": "wqes_gulet_o_10.jpg",
"name3": "qwsdf_gulet_o_11.jpg"
"name4": "sdce_gulet_o_12.jpg"
},
{
"id": "11",
"name1": "fsdsc_gulet_o_13.jpg"
"name2": "drfvc_gulet_o_14.jpg"
},
{
"id": "12",
"name1": "dsyjhk_gulet_o_15.jpg"
.
.
you want something like this:
foreach ($arr as $a) {
$new_arr[$a['id']]['names'][] = $a['name'];
}
I think creating indexes like name1, name2, name3 etc is not useful. You should create subarray with index names and there put your names. Try code like this:
<?php
$array = array(
array('id'=>1, 'name'=>'qwerty'),
array('id'=>2, 'name'=>'asdf'),
array('id'=>2, 'name'=>'hjkl'),
array('id'=>1, 'name'=>'cvbnm'),
array('id'=>1, 'name'=>'yuiop'),
);
$groups = array();
foreach($array as $singleRow)
{
if(!isset($groups[$singleRow['id']]))
$groups[$singleRow['id']] = array('id'=>$singleRow['id'], 'names'=>array());
$groups[$singleRow['id']]['names'][] = $singleRow['name'];
}
$json = json_encode(array_values($groups));
var_dump($json);
It outputs:
[{"id":1,"names":["qwerty","cvbnm","yuiop"]},{"id":2,"names":["asdf","hjkl"]}]
instead of -
$name1 = array('id'=>$name[0],'name'=>$name[1]);
$arr[] = $name1;
you can use something like -
$nameKey = 'name' . $name[0];
$name1 = array('id'=>$name[0],$nameKey =>$name[1]);
if (!empty($arr[$name[0]])) {
$arr[$name[0]] = $arr[$name[0]] + $name1;
}
else {
$arr[$name[0]] = $name1;
}
and after end of foreach() loop do -
$arr = array_values($arr);