从JSON数组中的键中查找值

时间:2018-07-04 08:50:20

标签: javascript arrays json

如何检查JSON数组中存在哪个JSON值?这是我的解决方案:

var jsonArr = "[{"F_1":"c4024087","F_2":"9a565962","F_3":"23026","F_4":"John","F_5":"Doe"},{"F_1":"dbb88908","F_2":"bc6fd3bb","F_3":"19727","F_4":"Mike","F_5":"Long"}]"

查找值:

var toFind = "9a565962";

var checkObj = jsonArr.find(o => o.F_2 == toFind );
  if (typeof checkObj === "undefined") {
    console.log("Value " + toFind + " not exists in Array. Do something!"); 
  } else (Object.keys(checkObj).length > 0) {
      console.log("Value " + toFind + " exists in Array. Do something else");
    }

这是在数组中查找值的最佳方法吗?

1 个答案:

答案 0 :(得分:0)

$jsondata = file_get_contents("/json/API.php");

$array = json_decode($jsondata,true);

foreach($array as $k=>$val):

if($id =! $val){   
 echo '<b>id: '.$k.'</b></br>';
    $keys = array_keys($val);
    foreach($keys as $key):
        echo '&nbsp;'.ucfirst($key).' = '.$val[$key].'</br>';
    endforeach;
endforeach;
}

也许可以解决您的问题最好的问候