Related
i want that type of output
{
"results": [
{
"text": "Group 1",
"children" : [
{
"id": 1,
"text": "Option 1.1"
},
{
"id": 2,
"text": "Option 1.2"
}
]
},
{
"text": "Group 2",
"children" : [
{
"id": 3,
"text": "Option 2.1"
}
]
}
]
}
here is my controller code but i can't match this type of json so please help me out
$att = array();
foreach ($products as $v ) {
if ( !isset($att[$v->supplier->name]) ) {
$att[$v->supplier->name] = array();
}
$att[$v->supplier->name][$v->id] = $v->prd_our_item_no.'-'.$v->prd_description;
}
i can't make key and text for each supplier can you guys help me out
I'm newbie in MongoDB and I'm really struggling to get the expected output. I have a set of records in collection, let's say:
{"_id": "id", "category": "Category A", "price": 100, "created_at": "2022-02-01 01:05:00"},
{"_id": "id", "category": "Category B", "price": 200, "created_at": "2022-02-01 01:10:00"},
{"_id": "id", "category": "Category B", "price": 150, "created_at": "2022-02-01 01:20:00"}
I want to group these by hour (created_at), and by categories, and get the sum of each category's price in a single line like this:
{"_id": "id", "Category A Total Price": 100, "Category B Total Price": 350, "created_at": "2022-02-01 01:00:00"}
Does this problem needs different query for each category and then merge? Or any solution you can share.
db.collection.aggregate([
{
"$match": {}
},
{
"$group": {
"_id": {
category: "$category",
created_at: {
$dateTrunc: {
date: { "$toDate": "$created_at" },
unit: "day"
}
}
},
"total": { "$sum": "$price" }
}
},
{
"$group": {
"_id": "$_id.created_at",
"obj": {
"$push": {
k: { "$concat": [ "$_id.category", " Total Price" ] },
v: "$total"
}
}
}
},
{
$replaceWith: {
$mergeObjects: [
{ "$arrayToObject": "$obj" },
{ created_at: "$_id" }
]
}
}
])
mongoplayground
I am trying to make the data format like below. for using jstree module
[{
"id":"1" ,
"parent_id":"0",
"text":"TEST",
"children":[{
"id":"2",
"parent_id":"1",
"text":"TEST1",
"children":[{
"id":"4",
"parent_id":"2",
"text":"TEST1-1"
}, {
"id":"5",
"parent_id":"2",
"text":"TEST1-2"
}, {
"id":"6",
"parent_id":"2",
"text":"TEST1-3"
}]
}, {
"id":"3",
"parent_id":"1",
"text":"TEST2",
"children":[{
"id":"7",
"parent_id":"3",
"text":"TEST2-1"
}, {
"id":"8",
"parent_id":"3",
"text":"TEST2-1"
}, {
"id":"9",
"parent_id":"3",
"text":"TEST2-1"
}]
}]
}]
but.. mine is
[{
"id":"1" ,
"parent_id":"0",
"text":"TEST",
"children":{
"id":"3",
"parent_id":"1",
"text":"\ud14c\uc2a4\ud2b82",
"children":{
"id":"9",
"parent_id":"3",
"text":"\ud14c\uc2a4\ud2b82-3"
}
}
},{
"children":{
"id":"1",
"parent_id":"0",
"text":"TEST1",
"children":{
"id":"3",
"parent_id":"1",
"text":"TEST2",
"children":{
"id":"9",
"parent_id":"3",
"text":"TEST2-3"
}
}
}
},{
"id":"2",
"parent_id":"1",
"text":"TEST1",
"children":{
"id":"6",
"parent_id":"2",
"text":"TEST1-3"
}
},{
"id":"3",
"parent_id":"1",
"text":"TEST2",
"children":{
"id":"9",
"parent_id":"3",
"text":"TEST2-3"
}
},{
"id":"4",
"parent_id":"2",
"text":"TEST1-1"
},{
"id":"5",
"parent_id":"2",
"text":"TEST1-2"
},{
"id":"6",
"parent_id":"2",
"text":"TEST1-3"
},{
"id":"7",
"parent_id":"3",
"text":"TEST2-1"
},{
"id":"8",
"parent_id":"3",
"text":"TEST2-2"
},{
"id":"9",
"parent_id":"3",
"text":"TEST2-3"
}]
I have no idea about the arrays or JSON. Here is the code:
$refs = array();
$list = array();
$sql = "
select id, parent_id, text
from code
where use_yn = 'Y'
$result = mysql_query($sql);
while($data = mysql_fetch_assoc($result)) {
$thisref = &$refs[$data['id']];
$thisref['id'] = $data['id'];
$thisref['parent_id'] = $data['parent_id'];
$thisref['text'] = iconv("euc-kr", "utf-8", $data['text']);
if ($data['id'] == 0) {
$list[ $data['id'] ] = &$thisref;
} else {
$refs[ $data['parent_id'] ]['children'] = &$thisref;
}
}
echo json_encode(array_values($refs));
Can anyone help me?
You are mixing different methods to form json for jsTree.
You either 1) use a multilevel structure using key children - you have that in the beginning of your result json, or 2) you use a one-level structure, don't use children keyword, just specify the parent node. I suggest you use the second way.
Instead of parent_id you should use simply parent.
Your output json contains duplicate entries with ids '1', '3', '9'. It is no good.
For root node use # as id.
To me your target json should look like below. To get it the correct php iteration snippet would probably look closer to as below, but you will have to specifically filter out duplicate entries which is not in the code yet.
Then you will get the expected result as in this demo - Fiddle.
while($data = mysql_fetch_assoc($result)) {
$thisref = &$refs[$data['id']];
$thisref['id'] = $data['id'] == 0 ? '#' : $data['id']; // if id is 0 , it's a root node, replace with '#'
$thisref['parent'] = $data['parent_id'] == 0 ? '#' : $data['parent_id'];// if parent id is 0, it's a root node, replace with '#'
$thisref['text'] = iconv("euc-kr", "utf-8", $data['text']);
$refs[] = &$thisref;
}
Correct json:
var data = [{
"id": "1",
"parent": "#",
"text": "TEST"
}, {
"id": "3",
"parent": "1",
"text": "테스트2"
}, {
"id": "9",
"parent": "3",
"text": "테스트2-3"
}, {
"id": "2",
"parent": "1",
"text": "TEST1"
}, {
"id": "6",
"parent": "2",
"text": "TEST1-3"
}, {
"id": "4",
"parent": "2",
"text": "TEST1-1"
}, {
"id": "5",
"parent": "2",
"text": "TEST1-2"
}, {
"id": "6",
"parent": "2",
"text": "TEST1-3"
}, {
"id": "7",
"parent": "3",
"text": "TEST2-1"
}, {
"id": "8",
"parent": "3",
"text": "TEST2-2"
}]
Also check docs at jsTree json reference
I am trying to iterate both index of JSON(Players and Buildings), so that i can a get new result in jQuery
I have two index of JSON one having information of Players and second index having information of Building related Player.
I want to Parse it so that i can get Player and its building name.
My Actual JSON result
{
"Players": [
{
"id": "35",
"building_id": "8",
"room_num": "101",
},
{
"id": "36",
"building_id": "9",
"room_num": "102",
},
{
"id": "37",
"building_id": "10",
"room_num": "103",
},
{
"id": "38",
"building_id": "11",
"room_num": "104",
}
],
"Buildings": [
{
"id": "8",
"name": "ABC"
},
{
"id": "9",
"name": "DEF"
},
{
"id": "10",
"name": "GHI"
},
{
"id": "11",
"name": "JKL"
}
]
}
Need this
"information": [
{
"player_id": "35",
"Buildings_name": "ABC"
},
{
"player_id": "36",
"Buildings_name": "DEF"
},
{
"player_id": "37",
"Buildings_name": "GHI"
},
{
"player_id": "38",
"Buildings_name": "JKL"
}
]
}
Here you go, this go for each player and check if there are buildings and will map them to new structure. This will not filter values for buildings that do not have mapping to players, and will not include the buildings with no players.
var x = {
"Players": [
{
"id": "35",
"building_id": "8",
"room_num": "101",
},
{
"id": "36",
"building_id": "9",
"room_num": "102",
},
{
"id": "37",
"building_id": "10",
"room_num": "103",
},
{
"id": "38",
"building_id": "11",
"room_num": "104",
}
],
"Buildings": [
{
"id": "8",
"name": "ABC"
},
{
"id": "9",
"name": "DEF"
},
{
"id": "10",
"name": "GHI"
},
{
"id": "11",
"name": "JKL"
}
]
};
var res = $.map(x.Players, function(item) {
return {
player_id: item.id,
building_name: $.grep(x.Buildings, function(i) {
return i.id == item.building_id
}).length != 0 ? $.grep(x.Buildings, function(i) {
return i.id == item.building_id
})[0].name : undefined
}
})
and if you want to filter values that do not have relationships e.g INNER join
var resInnerJoin = $.grep($.map(x.Players, function(item) {
return {
player_id: item.id,
building_name: $.grep(x.Buildings, function(i) {
return i.id == item.building_id
}).length != 0 ? $.grep(x.Buildings, function(i) {
return i.id == item.building_id
})[0].name : undefined
}
}), function(it) {
return it.building_name != undefined
})
If you need it in PHP :
$json = '{...}';
// create and PHP array with you json data.
$array = json_decode($json, true);
// make an array with buildings informations and with building id as key
$buildings = array();
foreach( $array['Buildings'] as $b ) $buildings[$b['id']] = $b;
$informations = array();
for ( $i = 0 ; $i < count($array['Players']) ; $i++ )
{
$informations[$i] = array(
'player_id' => $array['Players'][$i]['id'],
'Buildings_name' => $buildings[$array['Players'][$i]['building_id']]['name']
);
}
$informations = json_encode($informations);
var_dump($informations);
I have:
$all = array(
array('id'=>1, 'cat'=>'Main','type'=>'Name0'),
array('id'=>2, 'cat'=>'Main','type'=>'Name1'),
array('id'=>3, 'cat'=>'Main','type'=>'Name3'),
array('id'=>4, 'cat'=>'Main','type'=>'Name4'),
array('id'=>5, 'cat'=>'Secondary','type'=>'Name5'),
array('id'=>6, 'cat'=>'Secondary','type'=>'Name6'),
array('id'=>7, 'cat'=>'Secondary','type'=>'Name7'),
array('id'=>8, 'cat'=>'Other','type'=>'Name8'),
array('id'=>9, 'cat'=>'Other','type'=>'Name9'),
array('id'=>10, 'cat'=>'Other','type'=>'Name10'),
array('id'=>11, 'cat'=>'Other','type'=>'Name11'),
);
$result = array();
foreach($all as $array){
$result[$array['cat']][] = array('id'=>$array['id'],'type'=>$array['type']);
}
$json_type = json_encode($result);
Which returns:
{"Main":[{"id":"1","type":"name1"},{"id":"2","type":"name2"},{"id":"3","type":"name3"},{"id":"4","type":"name4"}],"Secondary":[{"id":"5","type":"name5"},{"id":"6","type":"name6"},{"id":"7","type":"name7"}],"Other":[{"id":"8","type":"name8"},{"id":"9","type":"name9"},{"id":"10","type":"name10"},{"id":"11","type":"name11"}]}
But I need it to return as:
[
{
"text": "Main",
"children": [
{
"id": "1",
"text": "name1"
},
{
"id": "2",
"text": "name2"
},
{
"id": "3",
"text": "name3"
},
{
"id": "4",
"text": "name4"
}
]
},
{
"text": "Secondary",
"children": [
{
"id": "5",
"text": "name5"
},
{
"id": "6",
"text": "name6"
},
{
"id": "7",
"text": "name7"
}
]
},
{
"text": "Other",
"children": [
{
"id": "8",
"text": "name8"
},
{
"id": "9",
"text": "name9"
},
{
"id": "10",
"text": "name10"
},
{
"id": "11",
"text": "name11"
}
]
}
]
To work with the select2 JQuery plugin, I'm working with.
the 'children' name doesn't matter, I think that's just a placeholder so it gets parsed correctly. I'm not sure how I would go about it, I've been trying str_replace() but even that hasn't been working out so great.
I would to it in 2 loops. The first one to group results by category, the 2nd one to format it to fit your needs:
$temp = array();
foreach($all as $array){
if (!isset($temp[$array['cat']])) {
$temp[$array['cat']] = array();
}
$temp[$array['cat']][] = array('id'=>$array['id'], 'type'=>$array['type']);
}
$result = array();
foreach ($temp as $key=>$value) {
$result[] = array('text'=>$key, 'children'=>$value);
}
echo json_encode($result);
This produces the following output:
[{"text":"Main","children":[{"id":1,"type":"Name0"},{"id":2,"type":"Name1"},{"id":3,"type":"Name3"},{"id":4,"type":"Name4"}]},{"text":"Secondary","children":[{"id":5,"type":"Name5"},{"id":6,"type":"Name6"},{"id":7,"type":"Name7"}]},{"text":"Other","children":[{"id":8,"type":"Name8"},{"id":9,"type":"Name9"},{"id":10,"type":"Name10"},{"id":11,"type":"Name11"}]}]