开发者社区 问答 正文

php读取json文件并查找其中的内容

screenshot
有这个么一个json格式个文件 x.json

现在 php中想读取json文件内容并按要求查找丛中的item
例如:通过A03 输出 dkey 的值
该怎么查找对应输出呢?求教!
screenshot

展开
收起
a123456678 2016-03-11 11:40:12 5244 分享 版权
1 条回答
写回答
取消 提交回答
  • 你的json文件有bom头,

    $json = file_get_contents('Qcode.json');
    $arr = json_decode(trim($json, chr(239) . chr(187) . chr(191)), true);
    $temp = array_column($arr, 'dkey', 'dvalue');
    echo $temp['A05'];
    if (! function_exists('array_column')) {
    
        function array_column(array $input, $columnKey, $indexKey = null)
        {
            $array = array();
            foreach ($input as $value) {
                if (! isset($value[$columnKey])) {
                    trigger_error("Key \"$columnKey\" does not exist in array");
                    return false;
                }
                if (is_null($indexKey)) {
                    $array[] = $value[$columnKey];
                } else {
                    if (! isset($value[$indexKey])) {
                        trigger_error("Key \"$indexKey\" does not exist in array");
                        return false;
                    }
                    if (! is_scalar($value[$indexKey])) {
                        trigger_error("Key \"$indexKey\" does not contain scalar value");
                        return false;
                    }
                    $array[$value[$indexKey]] = $value[$columnKey];
                }
            }

    screenshot

    2019-07-17 18:58:52
    赞同 展开评论