检查是否在php上退出json数组值

时间:2017-06-28 11:24:11

标签: php arrays json

大家好

我正在创建一个项目,我需要检查是否在PHP变量中定义的'x'值是否已建立在JSON steamid 数组......

以下是JSON结构的示例:

if (x value is founded on json->users steamid array) {

   $verified = 1;
   $teamspeakid = the founded steamid->teamspeakid;
   $nicknames = the founded steamid->nicknames;
   $addresses = the founded steamid->addresses;

} elseif (steamid value is not founded on json array) {

   $verified = 0;

}

我想到的PHP函数结构与此类似:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flaunder.flaunder, PID: 12300
java.lang.NoClassDefFoundError: com.flaunder.flaunder.Fragment_Home
at com.flaunder.flaunder.Pager2.getItem(Pager2.java:20)
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:109)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)              at android.support.v4.view.ViewPager.populate(ViewPager.java:1182)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)                              at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)                        at android.view.View.measure(View.java:16497)
    ................................

我希望你能帮助我: - )

1 个答案:

答案 0 :(得分:1)

您需要解码json数组,然后使用strpos检查字符串或字符是否存在。如下所示:

InputStreamReader

$jsonarr='{
"users": [
    {
        "steamid": "[X:X:XXXXXXXXX]",
        "teamspeakid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "nicknames": [
            "XXXXXXXXX"
        ],
        "addresses": [
            "XX.XX.XXX.XX"
        ]
    },
    {
        "steamid": "[Y:Y:YYYYYYYYY]",
        "teamspeakid": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY",
        "nicknames": [
            "YYYYYYYYY"
        ],
        "addresses": [
            "YY.YY.YYY.YY"
        ]
    },
    {
        "steamid": "[Z:Z:ZZZZZZZZZ]",
        "teamspeakid": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
        "nicknames": [
            "ZZZZZZZZZ"
        ],
        "addresses": [
            "ZZ.ZZ.ZZZ.ZZ"
        ]
    }

]}';

$arr=json_decode($jsonarr,true);
$searchstring='X';
foreach($arr['users'] as $key=>$value){
    if (strpos($value['steamid'], $searchstring) !== false) {
        $verified = 1;
    }else{
        $verified = 0;
    }
}