Aggregate mongo data php - php

I have collection of 113 documents. It is data from departments of university. There are 3 types of departments:
natural science departments => "chairtype" = "E",
humanities departments => "chairtype" = "G",
creative departments => "chairtype" = "T"
Every document contains fields with the same names:
"mijczjeqeo"
"vmfqvfjptu"
"fwtweaeeba"
...
and so on
113 people fill these fields from the web form every month. 1 person fills 1 document. After filling I make history of values in every document by php script.
"mijczjeqeo" value moves to "versions.0.content.mijczjeqeo"
"vmfqvfjptu" value moves to "versions.0.content.vmfqvfjptu"
"fwtweaeeba" value moves to "versions.0.content.fwtweaeeba"
...
and so on
and before saving last values...
all of "versions.0.content." values move to "versions.1.content."
all of "versions.1.content." values move to "versions.2.content."
all of "versions.2.content." values move to "versions.3.content."
...
and so on
I need to get the average value of each parameter for the last year grouped by chairtype
1 document for example ("Department of Organic Chemistry"):
{
"_id": ObjectId("52b85dfa32b6249513f15897"),
"atkswlntfd": 0,
"auwbsjqzir": 0,
"avqrnjzbgd": 0,
"awquatbduv": 0,
"axdducvoxb": 2.46,
"bkoldugcrp": 4,
"bzccjslewi": 0,
"cclwyezydc": 0,
"chairtype": "E",
"confirmed": "1",
"covfctuuhi": 0,
"dingrnyknr": 0,
"dkfknpzsnt": 0,
"dqetuhllse": 0,
"duorlxiqbw": 5,
"eayoicezsh": 20,
"esrfffruoy": 0,
"ewdunlkxue": 1,
"ewfshjnome": 0,
"exakqiudxg": 5,
"fabdcybqxu": 0,
"fsplxunmaf": 0,
"fubxmogyam": 1,
"fuzqrnwsks": 3,
"fwtweaeeba": 52.31,
"fybnnlojgb": 5,
"gdjheqrqcx": 0,
"gpupstzwjd": 6,
"gxilphzzcu": 0,
"hbahrruokf": 0,
"hbqnleclwp": 2,
"hchpoxxnwz": 0,
"hmorfnbfvf": 0,
"hqatnzynxb": 0,
"hrqssioxdv": 0,
"hvscavwupe": 0,
"hyzlbtnxil": 0,
"idzxqjoxgd": 0,
"ikxsvguboy": 0,
"ipjpwkbqnt": 0,
"izqighabwk": 9000000,
"jncncbplme": 3,
"jxkspszlrc": 1,
"kekarveuhb": 0,
"klyoglzriu": 0,
"kmvuelmdwe": 0,
"knxzfjwnax": 5,
"kqfhjboecc": 0,
"kqhojbwvmo": 0,
"lchogmhynm": 0,
"lmuwyeqvph": 7,
"lvcdbhisbx": 0,
"mijczjeqeo": 8,
"mpxzquzcat": 0,
"mqqoetqued": 0,
"muktdrzphw": 0,
"nceszojuvt": 0,
"nypnjqgxop": 1,
"ojklibfieg": 2,
"padotysmxb": 0,
"parent": "47de3176-bbc3-44e0-8063-8920ac56fdc8",
"pidwyruvfq": 35.08,
"pkeymzxsrj": 0,
"pnjtfvzwiv": 1,
"pqjnpoxmcx": 0,
"pyexnkjujx": 38,
"qfeqdvzssg": 0,
"qidslfqnwn": 0,
"qvjszkahdc": 0,
"qzoriqedoh": 0,
"rjicuyfsmt": 0,
"rqenalbuko": 40,
"rxkwogbxwu": 0,
"sbqqabqukn": 1,
"skhgbmucrp": 0,
"slewjrvgjn": 0,
"tidjarsatz": 0,
"title_ru": "Кафедра органической химии",
"tmbagkmlgb": 0,
"type": "chair",
"uploaded": "1",
"uqcdessbeu": 0,
"url": "http:\/\/www.herzen.spb.ru\/main\/structure\/fukultets\/him\/1208434887\/",
"uuid": "a0a39ace-694c-48c5-841d-8b351e5b91da",
"vacoxpronz": 0,
"vdjfydjrpa": 13,
"versions": {
"0": {
"_id": ObjectId("52dbbc4cfb0a29ce4a8b45bd"),
"content": {
"atkswlntfd": 0,
"auwbsjqzir": 0,
"avqrnjzbgd": 0,
"awquatbduv": 0,
"axdducvoxb": 3,
"bkoldugcrp": 4,
"bzccjslewi": 0,
"cclwyezydc": 0,
"confirmed": null,
"covfctuuhi": 0,
"dingrnyknr": 0,
"dkfknpzsnt": 0,
"dqetuhllse": 2,
"duorlxiqbw": 5,
"eayoicezsh": 20,
"esrfffruoy": 0,
"ewdunlkxue": 3,
"ewfshjnome": 0,
"exakqiudxg": 5,
"fabdcybqxu": 1,
"fsplxunmaf": 0,
"fubxmogyam": 1,
"fuzqrnwsks": 0,
"fwtweaeeba": 55,
"fybnnlojgb": 0,
"gdjheqrqcx": 0,
"gpupstzwjd": 6,
"gxilphzzcu": 0,
"hbahrruokf": 0,
"hbqnleclwp": 3,
"hchpoxxnwz": 1849020,
"hmorfnbfvf": 2,
"hqatnzynxb": 0,
"hrqssioxdv": 0,
"hvscavwupe": 0,
"hyzlbtnxil": 0,
"idzxqjoxgd": 2,
"ikxsvguboy": 1,
"ipjpwkbqnt": 0,
"izqighabwk": 1040000,
"jncncbplme": 1,
"jxkspszlrc": 1,
"kekarveuhb": 0,
"klyoglzriu": 1,
"kmvuelmdwe": 0,
"knxzfjwnax": 5,
"kqfhjboecc": 0,
"kqhojbwvmo": 0,
"lchogmhynm": 2,
"lmuwyeqvph": 7,
"lvcdbhisbx": 0,
"mijczjeqeo": 8,
"mpxzquzcat": 0,
"mqqoetqued": 1,
"muktdrzphw": 0,
"nceszojuvt": 0,
"nypnjqgxop": 11,
"ojklibfieg": 0,
"padotysmxb": 0,
"pidwyruvfq": 34,
"pkeymzxsrj": 0,
"pnjtfvzwiv": 0,
"pqjnpoxmcx": 0,
"pyexnkjujx": 38,
"qfeqdvzssg": 0,
"qidslfqnwn": 0,
"qvjszkahdc": 0,
"qzoriqedoh": 0,
"rjicuyfsmt": 0,
"rqenalbuko": 40,
"rxkwogbxwu": 0,
"sbqqabqukn": 1,
"skhgbmucrp": 0,
"slewjrvgjn": 0,
"tidjarsatz": 0,
"tmbagkmlgb": 1,
"uqcdessbeu": 0,
"vacoxpronz": 0,
"vdjfydjrpa": 11,
"vktxndqyhm": 0,
"vmfqvfjptu": 0,
"vofeebpgsc": 5,
"wewmtafjvk": 1,
"wfqdcsrltv": 0,
"wzmbxalguv": 0,
"xjllpdyool": 0,
"xjxbwynytx": 0,
"xmirihwycl": 0,
"xxrsnjlmfv": 0,
"ybgdjpexth": 0,
"ymkmkuxlrq": 0,
"yneoycjloj": 0,
"yogujlfvpb": 0,
"zahigfmqxb": 0,
"znqqhqkjte": 0,
"zyztudtziu": 3
},
"content_hash": "816090f397962f92f5329fa5bb0a9ec1",
"datetime": ISODate("2014-01-19T11:51:40.590Z"),
"description_ru": "Значение показателей за этап 1",
"label_ru": "Окончание этапа 1"
},
"1": {
"_id": ObjectId("5305372cfb0a2944638b45bc"),
"content": {
"atkswlntfd": 0,
"auwbsjqzir": 0,
"avqrnjzbgd": 1,
"awquatbduv": 0,
"axdducvoxb": 2.46,
"bkoldugcrp": 4,
"bzccjslewi": 1,
"cclwyezydc": 0,
"confirmed": "1",
"covfctuuhi": 0,
"dingrnyknr": 0,
"dkfknpzsnt": 0,
"dqetuhllse": 0,
"duorlxiqbw": 5,
"eayoicezsh": 20,
"esrfffruoy": 0,
"ewdunlkxue": 0,
"ewfshjnome": 0,
"exakqiudxg": 5,
"fabdcybqxu": 1,
"fsplxunmaf": 0,
"fubxmogyam": 1,
"fuzqrnwsks": 0,
"fwtweaeeba": 52.31,
"fybnnlojgb": 0,
"gdjheqrqcx": 0,
"gpupstzwjd": 6,
"gxilphzzcu": 0,
"hbahrruokf": 0,
"hbqnleclwp": 0,
"hchpoxxnwz": 0,
"hmorfnbfvf": 0,
"hqatnzynxb": 0,
"hrqssioxdv": 0,
"hvscavwupe": 0,
"hyzlbtnxil": 0,
"idzxqjoxgd": 0,
"ikxsvguboy": 0,
"ipjpwkbqnt": 0,
"izqighabwk": 0,
"jncncbplme": 0,
"jxkspszlrc": 1,
"kekarveuhb": 1,
"klyoglzriu": 0,
"kmvuelmdwe": 0,
"knxzfjwnax": 5,
"kqfhjboecc": 0,
"kqhojbwvmo": 0,
"lchogmhynm": 0,
"lmuwyeqvph": 7,
"lvcdbhisbx": 0,
"mijczjeqeo": 8,
"mpxzquzcat": 0,
"mqqoetqued": 0,
"muktdrzphw": 0,
"nceszojuvt": 0,
"nypnjqgxop": 0,
"ojklibfieg": 1,
"padotysmxb": 0,
"pidwyruvfq": 34.15,
"pkeymzxsrj": 0,
"pnjtfvzwiv": 0,
"pqjnpoxmcx": 0,
"pyexnkjujx": 38,
"qfeqdvzssg": 0,
"qidslfqnwn": 0,
"qvjszkahdc": 0,
"qzoriqedoh": 0,
"rjicuyfsmt": 0,
"rqenalbuko": 40,
"rxkwogbxwu": 0,
"sbqqabqukn": 1,
"skhgbmucrp": 0,
"slewjrvgjn": 0,
"tidjarsatz": 0,
"tmbagkmlgb": 0,
"uqcdessbeu": 0,
"vacoxpronz": 0,
"vdjfydjrpa": 11,
"visited": null,
"vktxndqyhm": 0,
"vmfqvfjptu": 0,
"vofeebpgsc": 4.77,
"wewmtafjvk": 0,
"wfqdcsrltv": 0,
"wzmbxalguv": 0,
"xjllpdyool": 0,
"xjxbwynytx": 0,
"xmirihwycl": 0,
"xxrsnjlmfv": 0,
"ybgdjpexth": 0,
"ymkmkuxlrq": 0,
"yneoycjloj": 0,
"yogujlfvpb": 0,
"zahigfmqxb": 0,
"znqqhqkjte": 0,
"zyztudtziu": 3.23
},
"content_hash": "d273fb095a7c08fef69fb90ec316fcb9",
"datetime": ISODate("2014-02-19T22:58:52.805Z"),
"description_ru": "Значение показателей за этап 2",
"label_ru": "Окончание этапа 2"
},
"2": {
"_id": ObjectId("532854a3fb0a2973718b45c6"),
"content": {
"atkswlntfd": 0,
"auwbsjqzir": 0,
"avqrnjzbgd": 2,
"awquatbduv": 0,
"axdducvoxb": 2.46,
"bkoldugcrp": 4,
"bzccjslewi": 0,
"cclwyezydc": 0,
"confirmed": "1",
"covfctuuhi": 0,
"dingrnyknr": 0,
"dkfknpzsnt": 0,
"dqetuhllse": 0,
"duorlxiqbw": 5,
"eayoicezsh": 20,
"esrfffruoy": 0,
"ewdunlkxue": 1,
"ewfshjnome": 0,
"exakqiudxg": 5,
"fabdcybqxu": 1,
"fsplxunmaf": 0,
"fubxmogyam": 1,
"fuzqrnwsks": 0,
"fwtweaeeba": 52.15,
"fybnnlojgb": 5,
"gdjheqrqcx": 0,
"gpupstzwjd": 6,
"gxilphzzcu": 0,
"hbahrruokf": 0,
"hbqnleclwp": 2,
"hchpoxxnwz": 0,
"hmorfnbfvf": 0,
"hqatnzynxb": 0,
"hrqssioxdv": 0,
"hvscavwupe": 0,
"hyzlbtnxil": 0,
"idzxqjoxgd": 0,
"ikxsvguboy": 0,
"ipjpwkbqnt": 0,
"izqighabwk": 0,
"jncncbplme": 0,
"jxkspszlrc": 1,
"kekarveuhb": 0,
"klyoglzriu": 0,
"kmvuelmdwe": 0,
"knxzfjwnax": 5,
"kqfhjboecc": 0,
"kqhojbwvmo": 0,
"lchogmhynm": 0,
"lmuwyeqvph": 7,
"lvcdbhisbx": 0,
"mijczjeqeo": 8,
"mpxzquzcat": 0,
"mqqoetqued": 0,
"muktdrzphw": 0,
"nceszojuvt": 0,
"nypnjqgxop": 0,
"ojklibfieg": 1,
"padotysmxb": 0,
"pidwyruvfq": 34.62,
"pkeymzxsrj": 0,
"pnjtfvzwiv": 0,
"pqjnpoxmcx": 0,
"pyexnkjujx": 38,
"qfeqdvzssg": 0,
"qidslfqnwn": 0,
"qvjszkahdc": 0,
"qzoriqedoh": 0,
"rjicuyfsmt": 2,
"rqenalbuko": 40,
"rxkwogbxwu": 0,
"sbqqabqukn": 1,
"skhgbmucrp": 0,
"slewjrvgjn": 0,
"tidjarsatz": 0,
"tmbagkmlgb": 0,
"uploaded": null,
"uqcdessbeu": 0,
"vacoxpronz": 0,
"vdjfydjrpa": 11,
"visited": true,
"vktxndqyhm": 0,
"vmfqvfjptu": 0,
"vofeebpgsc": 4.77,
"wewmtafjvk": 0,
"wfqdcsrltv": 0,
"wzmbxalguv": 0,
"xjllpdyool": 1,
"xjxbwynytx": 0,
"xmirihwycl": 0,
"xxrsnjlmfv": 0,
"ybgdjpexth": 0,
"ymkmkuxlrq": 0,
"yneoycjloj": 2,
"yogujlfvpb": 0,
"zahigfmqxb": 0,
"znqqhqkjte": 0,
"zyztudtziu": 3.23
},
"content_hash": "64adcf5534b5b1f77282a95f0b14ef99",
"datetime": ISODate("2014-03-18T14:13:55.593Z"),
"description_ru": "Значение показателей за этап 3",
"document_id": "52b85dfa32b6249513f15897",
"document_uuid": "a0a39ace-694c-48c5-841d-8b351e5b91da",
"label_ru": "Окончание этапа 3"
}
},
"visited": true,
"vktxndqyhm": 0,
"vmfqvfjptu": 0,
"vofeebpgsc": 4.77,
"wewmtafjvk": 1,
"wfqdcsrltv": 0,
"wzmbxalguv": 0,
"xjllpdyool": 1,
"xjxbwynytx": 0,
"xmirihwycl": 0,
"xxrsnjlmfv": 0,
"ybgdjpexth": 0,
"ymkmkuxlrq": 0,
"yneoycjloj": 0,
"yogujlfvpb": 0,
"zahigfmqxb": 0,
"znqqhqkjte": 0,
"zyztudtziu": 3.23
}
Now earliest array with history of data is "versions.2.", but 2 weeks later it will be "versions.3." and 6 weeks later it will be "versions.4." and so on...
**this is average of "versions.0.vofeebpgsc" values grouped by "chairtype":**
array(2) {
["result"]=>
array(3) {
[0]=>
array(2) {
["_id"]=>
string(1) "E"
["MID"]=>
float(1.3903333333333)
}
[1]=>
array(2) {
["_id"]=>
string(1) "T"
["MID"]=>
float(0.4)
}
[2]=>
array(2) {
["_id"]=>
string(1) "G"
["MID"]=>
float(0.72931034482759)
}
}
["ok"]=>
float(1)
}
function getMiddle($itemName,$chairType){
//return average of "versions.0.$itemName" value for "chairtype" = $chairType
switch ($chairType){
case 'E':
$chairType = 0;
break;
case 'T':
$chairType = 1;
break;
case 'G':
$chairType = 2;
break;
}
$m = new MongoClient();
$db = $m->foo_data;
$collection = new MongoCollection($db, 'documents');
$thisField = '$versions.content.'.$itemName;
$out = $collection->aggregate(
array('$match' => array('type' => 'chair')
),
array('$unwind' => '$versions'),
array('$group' => array( '_id' => '$_id',
'chairtype' => array('$first' =>'$chairtype'),
'versions' => array('$first' => '$versions')
)),
array('$group'=> array( '_id'=>'$chairtype',
'MID'=> array('$avg'=> $thisField)
))
);
return round ($out['result'][$chairType]['MID'],2);
}
echo getMiddle('vofeebpgsc','G'); //I use it in foreach cycle and send different input values
I need to get average value for each parameter between "versions.0.param_name" and "versions.11.param_name" in every document grouped by "chairtype" (it will be average from parameter history for last year for each type of university department)
I got this task because the average value of "versions.0.param_name" is not very good reflects the statistics.
for example... I have 60 documents with "chairtype" = "E" and now I have '0','1' and '2' arrays of history in versions
I need to calculate:
(
"versions.0.content.fwtweaeeba" +
"versions.1.content.fwtweaeeba" +
"versions.2.content.fwtweaeeba" +
the same fields for each of 59 documents
) / (60*3) = It is good average of "fwtweaeeba" parameter for "chairtype" = "E"
9 months later I will have 60 documents with "chairtype" = "E" and I will have '0','1','2','3','4','5','6','7',8','9','10','11', arrays of history in versions
I will need to calculate:
(
"versions.0.content.fwtweaeeba" +
"versions.1.content.fwtweaeeba" +
"versions.2.content.fwtweaeeba" +
"versions.3.content.fwtweaeeba" +
"versions.4.content.fwtweaeeba" +
"versions.5.content.fwtweaeeba" +
"versions.6.content.fwtweaeeba" +
"versions.7.content.fwtweaeeba" +
"versions.8.content.fwtweaeeba" +
"versions.9.content.fwtweaeeba" +
"versions.10.content.fwtweaeeba" +
"versions.11.content.fwtweaeeba" +
the same fields for each of 59 documents
) / (60*12) = It will be good average of "fwtweaeeba" parameter for "chairtype" = "E"
and so on for each "chairtype"
Can I do it with mongo db aggregation framework?
How can I do it?
Сan anyone suggest a good HOWTO about compiling difficult aggregation queries in MongoDB?

The data structure here is not a good implementation, there are lots of problems with how this is structured and it is completely unsuited to aggregation. The main problems here are:
Your structure does not actually use any arrays, right now it does not
All of the specific key names case a real problem, and this can be avoided.
As such the only way to traverse this sort of structure is using JavaScript with mapReduce.
Defining a mapper:
var mapper = function () {
for ( var n in this.versions ) {
for ( var k in this.versions[n].content ) {
if (
( k != 'confirmed' ) ||
( k != 'visited' ) )
emit(
{
type: this.chairtype,
key: k
},
this.versions[n].content[k]
);
}
}
};
So what this is doing is cycling through each of the versions entries and then also through everything on content. The key is emitted for each of the content keys you want as well as by the "chairtype" key. And the value is that matching value.
And then a reducer:
var reducer = function (key,values) {
return ( Array.sum( values ) != 0 )
? Array.sum( values ) / values.length : 0;
};
Which is just a simple way of producing a an average from all the values coming in for the mapper with the same key.
So while that should work nicely, what you should be doing is changing your structure. So in fact if you had something like this:
{
"_id": ObjectId("52b85dfa32b6249513f15897"),
"parent": "47de3176-bbc3-44e0-8063-8920ac56fdc8",
"type": "chair",
"chairtype": "E",
"content": [
{ "key": "atkswlntfd", "value": 0, "version": 0 },
{ "key": "auwbsjqzir", "value": 0, "version": 0 },
{ "key": "avqrnjzbgd", "value": 0, "version": 0 }
]
}
Or generally more or less in that form, the the aggregation operation becomes very simple:
db.collection.aggregate([
{ "$unwind": "$content" },
{ "$group": {
"_id": {
"chairtype": "$chairtype",
"key": "$content.key"
},
"average": { "$avg": "$content.value" }
}}
])
Or whatever other variation of this is required, but now it is made possible by changing the structure.
So without the document being structured differently you will need to use mapReduce to do this.

Related

Tilemap array in AS3

I have created a tilemap in AS3 that I need to save to a database, the system works absolutely fine. When the user creates an account, a map is automatically created for them (it's always the same sized map, with trees, rocks etc 11x11).
[18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 0, 0, 0, 0, 0, 0, 0, 0, 17],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 0, 0, 0, 0, 0, 0, 0, 0, 17],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17],
[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17],
[7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5],
How do I go about saving this to a mysqli database and then get the information and return it back to the AS3 script? I know how to post from AS3, and I know how to get information from a page. I've saved the entire Array under one Column called tileMap, then when I try and query tileMap from the table... nothing is returned. Is it because I am using the [ ] braces? I don't really know how to go about displaying this.
You can serialize/deserialize your data, IMHO keep data for database in JSON format would be nice, cause it's easy to convert such a data in Object ()
hence in Array too).
//this function converts data from Array to Object, we need Object for easy JSON conversation afterwards
function toObject(array:Array):Object
{
var obj:Object = {};
for (var i:int = 0; i < array.length; ++i)
{
obj[i] = array[i];
}
return obj;
}
//this converts Object To Array
function toArray(obj:Object):Array
{
var arr:Array = [];
for (var i in obj)
{
if (obj.hasOwnProperty(i))
{
arr.push(obj[i]);
}
}
return arr;
}
//it's you array
var arr:Array = [[18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 0, 0, 0, 0, 0, 0, 0, 0, 17], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 0, 0, 0, 0, 0, 0, 0, 0, 17], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17], [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17], [7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5]];
var jsonString:String = JSON.stringify(toObject(arr)); //we got JSON string which for example could be saved as TEXT data in MySQL via PHP
var onceMoreArray:Array=toArray(JSON.parse(jsonString)); //We can convert JSON string back to array
Hope this method would help.
Create a string from your matrix.
[1,0,2],
[2,3,4],
[0,1,5]
become
1:0:2|2:3:4|0:1:5
To read you can use the split function, first with '|' and then with ':'

Why this causes syntax error at PhP?

private static $columns = [
self::COUNTRY_CODE => [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
self::COUNTRY_NAME => [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
self::REGION_NAME => [0, 0, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
self::CITY_NAME => [0, 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16],
self::LATITUDE => [0, 0, 0, 0, 20, 20, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
self::LONGITUDE => [0, 0, 0, 0, 24, 24, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24],
self::ISP => [0, 12, 0, 20, 0, 28, 20, 28, 0, 32, 0, 36, 0, 36, 0, 36, 0, 36, 28, 36, 0, 36, 28, 36],
self::DOMAIN_NAME => [0, 0, 0, 0, 0, 0, 24, 32, 0, 36, 0, 40, 0, 40, 0, 40, 0, 40, 32, 40, 0, 40, 32, 40],
self::ZIP_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 28, 28, 28, 28, 0, 28, 28, 28, 0, 28, 0, 28, 28, 28, 0, 28],
self::TIME_ZONE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 32, 28, 32, 32, 32, 28, 32, 0, 32, 32, 32, 0, 32],
self::NET_SPEED => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 44, 0, 44, 32, 44, 0, 44, 0, 44, 0, 44],
self::IDD_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 48, 0, 48, 0, 48, 36, 48, 0, 48],
self::AREA_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 52, 0, 52, 0, 52, 40, 52, 0, 52],
self::WEATHER_STATION_CODE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 56, 0, 56, 0, 56, 0, 56],
self::WEATHER_STATION_NAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 60, 0, 60, 0, 60, 0, 60],
self::MCC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 64, 0, 64, 36, 64],
self::MNC => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 68, 0, 68, 40, 68],
self::MOBILE_CARRIER_NAME => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 72, 0, 72, 44, 72],
self::ELEVATION => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 76, 0, 76],
self::USAGE_TYPE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 80],
];
I am downloading IP2Location at https://www.ip2location.com/developers/php . The PhP module is like that.
It causes syntax error.
Is this a new syntax that works for PhP 5 and not below or what?
Is this a new syntax that works for PhP 5 and not below or what?
Yes, that's short array syntax which is there for PHP 5.4+
Quoting from the manual
PHP 5.4.0 offers a wide range of new features:
One of which is
Short array syntax has been added, e.g. $a = [1, 2, 3, 4]; or $a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];.
As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].
Example #1 A simple array
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// as of PHP 5.4
$array = [
"foo" => "bar",
"bar" => "foo",
];
?>

Generate multiple json files from text file

I am in need of help on how to generate multiple JSON files from data from a text file. I have a JSON file "file1.json" with following structure:
{
"level": [
6, 6, 4, 4, 2, 2, 3, 3, 6, 6,
0, 2, 3, 3, 6, 6, 4, 4, 3, 3,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
}
I have a text file with similar data but not in json format and separated by a line:
8, 8, 8, 8, 8, 8, 8, 8,
0, 6, 6, 4, 4, 2, 2, 3,
2, 2, 3, 3, 6, 6, 4, 4,
0, 0, 0, 0, 0, 0, 0, 0,
6, 6, 4, 4, 2, 2, 3, 3,
0, 6, 6, 4, 4, 2, 2, 4,
0, 0, 0, 3, 3, 3, 0, 0,
0, 0, 0, 0, 4, 0, 0, 0,
0, 7, 7, 7, 7, 7, 7, 0,
0, 0, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 0,
I am looking for a solution to help extract the data and create a json file in the above format with file name incremental like file2.json, file3.json etc but have not been able to find a solution to this as I have over 600 of these file to create.
Based on online search, it appears PHP might do but I have no knowledge of PHP. Any help or pointing me to a possible solution would be appreciated.
Not tested with the foreach loop, but by setting the $content static.
<?
$path = "/path/to/files/";
$files = array_diff(scandir($path), array('..', '.'));
foreach($file in $files)
{
$content = file_get_contents($path . $file, true);
/*
$content = "8, 8, 8, 8, 8, 8, 8, 8,
0, 6, 6, 4, 4, 2, 2, 3,
2, 2, 3, 3, 6, 6, 4, 4,
0, 0, 0, 0, 0, 0, 0, 0,";
*/
$content = preg_replace('/[^\d,]/i', '', $content);
$_content = array_filter(explode(',', $content));
echo json_encode(array('level' => array_values($_content)));
}
?>

Highcharts graph X-axis label for different date ranges

I have written a below code which generates a area graph for selected dates (to and from dates).
$(document).ready(function() {
var options = {
"series": [{
"showInLegend": false,
"color": "#D0D0D0",
"name": "Revenue",
"data": [0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}],
"credits": {
"enabled": false
},
"chart": {
"renderTo": "highchart_id",
"defaultSeriesType": "area",
"shadow": true
},
"title": {
"text": null,
"align": "center",
"x": 0,
"y": 20
},
"xAxis": {
"type": "datetime",
"dateTimeLabelFormats": {
"month": "%e. %b",
"year": "%b"
},
"labels": {
"enabled": "false"
}
},
"legend": {
"enabled": true
},
"yAxis": {
"title": {
"text": ""
},
"labels": {
"enabled": true
}
},
"plotOptions": {
"area": {
"stacking": "normal",
"lineColor": "#3E3E3E",
"lineWidth": 3,
"marker": {
"lineWidth": "1",
"lineColor": "#3E3E3E",
"states": {
"hover": {
"enabled": true,
"radius": 1
}
}
}
},
"series": {
"pointStart": 1335823200000,
"pointInterval": 86400000
}
}
};
var chart = new Highcharts.Chart(options);
});​
The interval of dates on x-axis is coming one day less. here the series value 2 is for 10 may whereas it is showing for 9 may in graph.
I am new to this highchart, can anybody advice the solution please and let me known if you need any more clarification on the problem.
Try adding this code before declaring the chart
Highcharts.setOptions({
time: {
useUTC: false
}
});
By default highchart used UTC times. By adding the code above it will use the time zone of the browser.

My script is removing white spaces

This is a little "cut" from the data.txt:
(7220, 'Miecz Swiatla Ksiezyca +', 'Miecz Swiatla Ksiezyca +', 1, 0, 0, 2, 32, 1, 16, '', 30000, 100, 7221, 19, 0, 5, 1, 80, 0, 0, 7, 15, 17, 2, 0, 0, 0, 90, 110, 5, 67, 117, -1, -1, -1, -1, -1, -1, 0, 3, -1),
(2, 'ŔÎĹͳ׼ųÎ070905-121713 ', 'Polska Wersja', 0, 0, 0, 1, 0, 0, 0, '', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, 0, 0, 0),
(10, 'µµ+0 ', 'Miecz +0', 1, 0, 0, 2, 32, 1, 16, '', 0, 0, 11, 1, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 0, -1, -1, -1, -1, -1, -1, 0, 1, 0),
(11, 'µµ+1 ', 'Miecz +1', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 12, 2, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 7, -1, -1, -1, -1, -1, -1, 0, 1, 0),
(12, 'µµ+2 ', 'Miecz +2', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 13, 3, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 14, -1, -1, -1, -1, -1, -1, 0, 1, 0),
(13, 'µµ+3 ', 'Miecz +3', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 14, 4, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 21, -1, -1, -1, -1, -1, -1, 0, 1, 0),
(14, 'µµ+4 ', 'Miecz +4', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 15, 5, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 28, -1, -1, -1, -1, -1, -1, 30, 1, 0),
(15, 'µµ+5 ', 'Miecz +5', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 16, 6, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 35, -1, -1, -1, -1, -1, -1, 40, 1, 0),
(16, 'µµ+6 ', 'Miecz +6', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 17, 7, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 42, -1, -1, -1, -1, -1, -1, 50, 1, 0),
(17, 'µµ+7 ', 'Miecz +7', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 18, 8, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 49, -1, -1, -1, -1, -1, -1, 65, 1, 0),
(18, 'µµ+8 ', 'Miecz +8', 1, 0, 0, 2, 32, 1, 16, '', 100, 0, 19, 9, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 56, -1, -1, -1, -1, -1, -1, 80, 1, 0),
(19, 'µµ+9 ', 'Miecz +9', 1, 0, 0, 2, 32, 1, 16, '', 2000, 0, 0, 0, 0, 15, 1, 0, 0, 0, 7, 22, 0, 0, 0, 0, 0, 15, 19, 13, 15, 63, -1, -1, -1, -1, -1, -1, 100, 1, 0),
This is a script I use, to generate a query to the MySQL:
header('Content-type: text/html; charset=windows-1250');
$data = file("data.txt"); // returns an array of the lines
foreach($data as $key => $val){
$temp = explode(',', $val);
$id = substr($temp[0], 1);
$name = $temp[1];
$query = 'UPDATE `item_proto`
SET `name` = ' . $name . '
WHERE `vnum` = ' . $id . ';';
echo $query."<Br/>";
}
The output is following:
UPDATE `item_proto` SET `name` = 'Miecz Swiatla Ksiezyca +' WHERE `vnum` = 7220;
UPDATE `item_proto` SET `name` = 'ŔÎĹͳ׼ųÎ070905-121713 ' WHERE `vnum` = 2;
UPDATE `item_proto` SET `name` = 'µµ+0 ' WHERE `vnum` = 10;
UPDATE `item_proto` SET `name` = 'µµ+1 ' WHERE `vnum` = 11;
UPDATE `item_proto` SET `name` = 'µµ+2 ' WHERE `vnum` = 12;
UPDATE `item_proto` SET `name` = 'µµ+3 ' WHERE `vnum` = 13;
UPDATE `item_proto` SET `name` = 'µµ+4 ' WHERE `vnum` = 14;
UPDATE `item_proto` SET `name` = 'µµ+5 ' WHERE `vnum` = 15;
UPDATE `item_proto` SET `name` = 'µµ+6 ' WHERE `vnum` = 16;
UPDATE `item_proto` SET `name` = 'µµ+7 ' WHERE `vnum` = 17;
UPDATE `item_proto` SET `name` = 'µµ+8 ' WHERE `vnum` = 18;
UPDATE `item_proto` SET `name` = 'µµ+9 ' WHERE `vnum` = 19;
The above output, has only one space at the end, but the script is removing all other white spaces, as the name is supposed to be 24 characters count (including the white spaces).
So why is my script removing the white spaces from the end of a name attr?
It's not.
But if you're viewing those queries in your web browser and haven't changed the Content-Type from text/html, then your browser is collapsing whitespaces as it does in HTML.
Either add header("Content-type: text/plain"); to the start of your script (and swap <br/> for \n), or just "View Source" in your browser to see the verbatim output.
I think it is happening as you have set Content-type as text/html. HTML treats multiple white spaces as one. So try using plain text content type.

Categories