doumowu7371
2018-08-11 05:56
浏览 103

循环遍历json文件以在PHP中查找特定值

My json file look likes

myjson.json

[
{"name":"category_01","data":
[
{"id":"1","word":"ma","given_value":"1"},
{"id":"3","word":"me","given_value":"1"},
] }
[
{"name":"category_02","data":
[
{"id":"1","word":"vea","given_value":"1"},
{"id":"3","word":"ve","given_value":"1"},
] }

So what I want here is, check whether a particular value is in this json array using php. Assume that,

myphp.php

$word = 've';

if this value is in the above array, should find is it in category_01 or category_02. and also want to find given_value of matching word. I just tried in this way,

$data = file_get_contents ("myjson.json");
$json = json_decode($data, true);

foreach($arr as $item) { 
$uses = ($item['word']= $word); 
}

This doesn't work. How can I fix this, Please help me!

图片转代码服务由CSDN问答提供 功能建议

我的json文件看起来像

myjson.json \ ñ

 <代码> [\ N { “名称”: “category_01”, “数据”:\ N [\ N { “ID” 为 “1”, “单词”: “MA”, “given_value”  :“1”},
 {“id”:“3”,“word”:“me”,“given_value”:“1”},
 
}} 
 [
 {“name”:“category_02  “ ”数据“:\ N [\ N { ”ID“ 为 ”1“, ”单词“: ”VEA“, ”given_value“: ”1“},\ N { ”ID“: ”3“,” 字 “:”ve“,”given_value“:”1“},
 
}} 
   
 
 

所以我想要的是,检查特定值是否在 这个json数组使用php。 假设,

myphp.php

  $ word ='ve'; 
   
  
 

如果此值在上面的数组中,则应在 category_01 category_02 中找到它。 并且还想找到匹配单词的 given_value 。 我刚试过这种方式,

  $ data = file_get_contents(“myjson.json”)  ; 
 $ json = json_decode($ data,true); 
 
foreach($ arr as $ item){
 $ uses =($ item ['word'] = $ word);  
} 
   
 
 

这不起作用。 我该如何解决这个问题,请帮助我!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douduan6731 2018-08-11 06:11
    已采纳

    First of all, the JSON you posted is invalid. I think it should look like this:

    [
        {
            "name": "category_01",
            "data": [{
                    "id": "1",
                    "word": "ma",
                    "given_value": "1"
                },
                {
                    "id": "3",
                    "word": "me",
                    "given_value": "1"
                }
            ]
        },
        {
            "name": "category_02",
            "data": [{
                    "id": "1",
                    "word": "vea",
                    "given_value": "1"
                },
                {
                    "id": "3",
                    "word": "ve",
                    "given_value": "1"
                }
            ]
        }
    ]
    

    Try using on online tool like https://jsonlint.com/ to check your JSON. (if you get errors i recommend build the json again from scratch).

    I also recommend checking your json before using it:

    if ($arr === null && json_last_error() !== JSON_ERROR_NONE) {
        die("incorrect json data");
    }
    

    To get your value you have to KNOW how your data looks like and then process it:

    foreach($arr as $category) { 
        foreach($category['data'] as $data) { 
            if(strstr($data['word'], $word))
            echo $category['name'].' '.$data['word'].' '.$data['given_value']."
    ";
        }
    }
    
    点赞 评论

相关推荐 更多相似问题