2017-04-13 33 views
0

在這裏,我有一個數組,在這個數組我還有一個叫陣列studentabsentId,這裏其實studentabsentId 1是2017年4月11日2017-缺席04-12如何找到特定的價值存在於JSON字符串

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年4月11日和2017年4月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

Mr Jay Blanchard,我不知道該怎麼做,所以你可以請更新答案 –

+1

我投票結束這個問題作爲題外話,因爲StackOverflow不是一個免費的衆包代碼生成器。 –

回答

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 VERS離子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); 
+1

Mr @aaaaaaay先生我需要在PHP中的答案,jsfiddle鏈接沒有任何顯示 –

+0

哎呀,剛剛看到一個php標籤,編輯 – Jaaaaaaay

+0

先生@Jaaaaaaay我無法做到,你能請更新你的答案在php –

相關問題