dqtu14636 2017-10-16 22:12
浏览 186

我如何从函数内部的foreach循环中的数组返回值

Good day people, My problem is this, i have a json format file and i decoded it with php's json_decode() from this json file i want to iterate through it and store all the definitions element in a variable which i will return at the end of my function. i have tried

function dictSearch(){
        $file = file_get_contents('wiki.json');
        $input = json_decode($file);
        $ans = '';
        $error = 'No definition for that word';
        foreach ($input->results as $result){
            foreach ($result as $value=>$key){
                if ($value === 'definition'){
                    $answer[] = $key.'
';
                }
            }
        }
        return print $answer;
    }

but it dosen't work. Take a look at the json file.

{
  "word": "rat",
  "results": [
    {
      "definition": "someone who works (or provides workers) during a strike",
      "partOfSpeech": "noun",
      "synonyms": [
        "blackleg",
        "scab",
        "strikebreaker"
      ],
      "typeOf": [
        "worker"
      ]
    },
    {
      "definition": "a person who is deemed to be despicable or contemptible",
      "partOfSpeech": "noun",
      "synonyms": [
        "bum",
        "crumb",
        "dirty dog",
        "git",
        "lowlife",
        "puke",
        "rotter",
        "scum bag",
        "skunk",
        "so-and-so",
        "stinker",
        "stinkpot"
      ],
      "typeOf": [
        "unpleasant person",
        "disagreeable person"
      ],
      "examples": [
        "kill the rat"
      ]
    },
    {
      "definition": "take the place of work of someone on strike",
      "partOfSpeech": "verb",
      "synonyms": [
        "blackleg",
        "fink",
        "scab"
      ],
      "typeOf": [
        "do work",
        "work"
      ],
      "derivation": [
        "ratter"
      ]
    },
    {
      "definition": "give away information about somebody",
      "partOfSpeech": "verb",
      "synonyms": [
        "betray",
        "denounce",
        "give away",
        "grass",
        "shit",
        "shop",
        "snitch",
        "stag",
        "tell on"
      ],
      "typeOf": [
        "inform"
      ],
      "hasTypes": [
        "sell someone out"
      ],
      "derivation": [
        "ratting",
        "ratter"
      ]
    },
    {
      "definition": "one who reveals confidential information in return for money",
      "partOfSpeech": "noun",
      "synonyms": [
        "betrayer",
        "blabber",
        "informer",
        "squealer"
      ],
      "typeOf": [
        "source",
        "informant"
      ],
      "hasTypes": [
        "nark",
        "grass",
        "fink",
        "copper's nark",
        "canary",
        "sneak",
        "sneaker",
        "snitch",
        "snitcher",
        "stool pigeon",
        "stoolie",
        "stoolpigeon",
        "supergrass"
      ]
    },
    {
      "definition": "any of various long-tailed rodents similar to but larger than a mouse",
      "partOfSpeech": "noun",
      "typeOf": [
        "rodent",
        "gnawer"
      ],
      "hasTypes": [
        "oryzomys palustris",
        "mole rat",
        "norway rat",
        "brown rat",
        "rice rat",
        "rattus rattus",
        "rattus norvegicus",
        "roof rat",
        "bandicoot rat",
        "black rat",
        "pocket rat",
        "jerboa rat"
      ],
      "derivation": [
        "ratty"
      ]
    },
    {
      "definition": "a pad (usually made of hair) worn as part of a woman's coiffure",
      "partOfSpeech": "noun",
      "typeOf": [
        "pad"
      ],
      "partOf": [
        "hairdo",
        "coif",
        "coiffure",
        "hair style",
        "hairstyle"
      ]
    },
    {
      "definition": "catch rats, especially with dogs",
      "partOfSpeech": "verb",
      "typeOf": [
        "capture",
        "catch"
      ],
      "derivation": [
        "ratter"
      ]
    },
    {
      "definition": "desert one's party or group of friends, for example, for one's personal advantage",
      "partOfSpeech": "verb",
      "typeOf": [
        "desert",
        "defect"
      ],
      "derivation": [
        "ratter"
      ]
    },
    {
      "definition": "employ scabs or strike breakers in",
      "partOfSpeech": "verb",
      "inCategory": [
        "manufacture",
        "industry"
      ],
      "typeOf": [
        "hire",
        "engage",
        "employ"
      ],
      "derivation": [
        "ratter"
      ]
    },
    {
      "definition": "give (hair) the appearance of being fuller by using a rat",
      "partOfSpeech": "verb",
      "typeOf": [
        "fill out",
        "pad"
      ]
    }
  ],
  "syllables": {
    "count": 1,
    "list": [
      "rat"
    ]
  },
  "pronunciation": {
    "all": "ræt"
  },
  "frequency": 4.49
}

What i want to do is iterate through the json file and store all the values of definition in a variable which i can be able to return from my function

  • 写回答

1条回答 默认 最新

  • dpxnrx11199 2017-10-16 22:16
    关注

    You may find array_column very handy. It will return you an array with the values of a specific "column" (i.e. element of the array)

    function dictSearch(){
        $file = file_get_contents('wiki.json');
        $input = json_decode($file, 1); // force array
        return array_column($file['results'], 'definition');
    }
    

    you will get (online sandbox):

    array(11) {
      [0]=>
      string(55) "someone who works (or provides workers) during a strike"
      [1]=>
      string(55) "a person who is deemed to be despicable or contemptible"
      [2]=>
      string(43) "take the place of work of someone on strike"
      [3]=>
      string(36) "give away information about somebody"
      [4]=>
      string(60) "one who reveals confidential information in return for money"
      [5]=>
      string(69) "any of various long-tailed rodents similar to but larger than a mouse"
      [6]=>
      string(63) "a pad (usually made of hair) worn as part of a woman's coiffure"
      [7]=>
      string(32) "catch rats, especially with dogs"
      [8]=>
      string(81) "desert one's party or group of friends, for example, for one's personal advantage"
      [9]=>
      string(34) "employ scabs or strike breakers in"
      [10]=>
      string(57) "give (hair) the appearance of being fuller by using a rat"
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记