简体   繁体   中英

Find value from key in JSON Array

How can i check which JSON value exist in JSON Array? Here are my solution:

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"}]"

Find the value:

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");
    }

Is this the best way to find a value in a array?

$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;
}

Probably it will solve your problem Best Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM