这里我有一个数组,在这个数组中我还有一个名为 studentabsentId 的数组,这里实际上在 2017-04-11 和上没有studentabsentId 1 2017年4月12日
echo $json = json_encode($optionsList);
结果
[
{
"studentabsentId": [
"1",
"2"
],
"studentAbsentDate": "2017-04-11"
},
{
"studentabsentId": [
"1"
],
"studentAbsentDate": "2017-04-12"
},
{
"studentabsentId": [
"2"
],
"studentAbsentDate": "2017-04-13"
}
]
studentabsentId 2缺少2017-04-11和2017-04-13,现在我想做的意思是id 2是哪个日期不存在,
预期结果
{
"status": "Success",
"data": [
{
"studentAbsentId": "2"
"studentAbsentDate": "2017-04-11",
"response":"absent"
},
{
"studentAbsentId": "2"
"studentAbsentDate": "2017-04-13",
"response":"absent"
}
]
}
我的更新代码
$json = json_encode($optionsList);
$result = array();
for($i = 0; $i<count($json); $i++){
for($j = 0; $j<count($json[$i]->studentabsentId); $j++){
if($json[$i]->studentabsentId[$j] === "2"){
array_push($result,
(object)[
"studentabsentId" => $json[$i]->studentabsentId[$j],
"studentAbsentDate" => $json[$i]->studentAbsentDate,
]);
}
}
}
//echo(print_r($result));
$expected_result = (object)[
"status" => "Success",
"data" => $result
];
echo(print_r($expected_result));
答案 0 :(得分:0)
PHP版本,沙箱http://sandbox.onlinephpfunctions.com/code/c7991f23dc9bdd304eb50e27a06b68495564fa94
:
<?php
$json = array(
(object) [
"studentabsentId" => array("1", "2"),
"studentAbsentDate" => "2017-04-11"
],
(object) [
"studentabsentId" => array("1"),
"studentAbsentDate" => "2017-04-12"
],
(object) [
"studentabsentId" => array("2"),
"studentAbsentDate" => "2017-04-13"
]
);
//echo(print_r($json));
$result = array();
for($i = 0; $i<count($json); $i++){
for($j = 0; $j<count($json[$i]->studentabsentId); $j++){
if($json[$i]->studentabsentId[$j] === "2"){
array_push($result,
(object)[
"studentabsentId" => $json[$i]->studentabsentId[$j],
"studentAbsentDate" => $json[$i]->studentAbsentDate,
]);
}
}
}
//echo(print_r($result));
$expected_result = (object)[
"status" => "Success",
"data" => $result
];
echo(print_r($expected_result));
?>
JS版jsfiddle https://jsfiddle.net/czdqqegq/
var json = [
{
"studentabsentId": [
"1",
"2"
],
"studentAbsentDate": "2017-04-11"
},
{
"studentabsentId": [
"1"
],
"studentAbsentDate": "2017-04-12"
},
{
"studentabsentId": [
"2"
],
"studentAbsentDate": "2017-04-13"
}
];
var result = [];
for(var i = 0; i<json.length; i++){
for(var j =0; j<json[i].studentabsentId.length; j++){
if(json[i].studentabsentId[j] === "2"){ /*Filter out where ID is 2*/
result.push({
"studentAbsentId" : json[i].studentabsentId[j],
"studentAbsentDate" : json[i].studentAbsentDate
});
}
}
}
var expected_results = {
"status" : "Success",
"data" : result
}
console.log(expected_results);