我可能会遗漏一些显而易见的东西但是可以使用<?PHP
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "lms";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$empid = (isset($_POST['empid']) ? $_POST['empid'] : '');
$month = (isset($_POST['month']) ? $_POST['month'] : '');
$month = "2015-sep";
$month1 = date('F', strtotime('$month'));
//echo $month1;
$year = date('Y',strtotime('$month'));
$monthStart = date("Y-m-1") . "<br/>";
$num = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));
$monthEnd = date("Y-m-".$num)."<br/>";
//echo "$num";
$months = date('M');
$years = date ('Y');
$leave = 0;
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `startdate`, `enddate`,`duration` FROM `leaves` WHERE `employee` = '2' AND `status` = '3'
AND `startdate` > '01-09-2015' AND `enddate` < '30-09-2015' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$start = ($row['startdate']);
$end = ($row['enddate']);
$startcount = ['COUNT(`employee`)'];
//$durationlvs = ($row['duration']);
echo "sdate:$start,edate:$end</br>";
//echo "lvsdur:$durationlvs";
}
function getLeavesInPeriod($start, $end) {
$date = new DateTime($start);
$endDate = new DateTime($end);
$leaves = array();
while( $date <= $endDate ) {
$year = $date->format('Y');
$month = $date->format('M');
if(!array_key_exists($year, $leaves))
$leaves[$year] = array();
if(!array_key_exists($month, $leaves[$year]))
$leaves[$year][$month] = 0;
$leaves[$year][$month]++;
$date->modify("+1 day");
}
return $leaves;
}
$leaves = getLeavesInPeriod($start,$end);
$noofleaves=$leaves[$years][$months];
echo $noofleaves;
$conn->close();
}
?>
创建这样的JSON吗?
json_encode
更新
我必须从一开始就注意到我知道我所要求的味道很糟糕。然而,这正是SOLR所要求的。见http://wiki.apache.org/solr/UpdateJSON
答案 0 :(得分:4)
不,json_encode不会为您提供重复键的结果,因为您编码的对象/数组不能拥有一个属性/键的多个值,但您可以存储相同的数据有两种不同的方式。最自然的可能是:
{
"foo": ["bar", "baz"]
}
您还可以执行以下操作:
[
{
"key": "foo",
"value": "bar"
},
{
"key": "foo",
"value": "baz"
}
]
如果你必须获得SOLR的语法,你可以通过组合多次调用json_encode来获得它,虽然它不是很漂亮:
$foo1 = [ 'foo' => 'bar' ];
$foo2 = [ 'foo' => 'baz' ];
echo rtrim( json_encode( $foo1 ), '}' ) . ',' . ltrim( json_encode( $foo2 ), '{' );
// {"foo":"bar","foo":"baz"}