0

I have json data like this:

{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}

I need to extract from json only keywords. I try without success with code:

$json = '{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}';

foreach ($json['result'] as $keywords)
{
    echo $keywords['result']['keyword'];
}
Eduard Dimitrov
  • 152
  • 2
  • 10

2 Answers2

5

Seems, you forgot to decode the string with json_decode. In addition, your likely would to loop over [result][hits] and not just [result]

$json = '{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}';
$json = json_decode($json, true);

foreach ($json['result']['hits'] as $hit)
{
    echo $hit['keyword'];
}
Philipp
  • 15,377
  • 4
  • 35
  • 52
-1

TRY

    $json = '{"result":{"total":19,"hits":[{"keyword":"sidney webb"},{"keyword":"dr webb"}]},"status_msg":"OK","status_code":200,"left_lines":1196851}';
    $jsonp =json_decode($json,TRUE);

    foreach ($jsonp['result'] as $val)
    {
        if(is_array($val) ){
        foreach ($val as $k)
        {
            echo  $k['keyword'];

        }
        }

    }
safin chacko
  • 1,345
  • 1
  • 11
  • 18