skip to Main Content

I need to show on page some data from json_decode, but I receive an illegal offset error.

$json = '{"result":{"keyword":"seo",
                    "cost":0.54,
                    "concurrency":25,
                    "found_results":422000000,
                    "region_queries_count":1900,
                    "region_queries_count_wide":0,
                    "region_queries_count_phrase":0,
                    "types":["related_search","pic"],
                    "geo_names":[],
                    "social_domains":["wikipedia","linkedin"],
                    "right_spelling":null,
                    "keyword_length":1,
                    "lang":null,
                    "keyword_id":"647713",
                    "suggestions_count":12528,
                    "keywords_count":1637
                    },
          "status_msg":"OK",
          "status_code":200,
          "left_lines":99787
         }';
$obj = json_decode($json, true);
if (is_array($obj['result'])) {
foreach ($obj['result'] as $keywords) {

echo '<li>Keyword: "'.$keywords['keyword'].'", Adwords CPC: <b>'.$keywords['cost'].'</b> USD, Concurrency: <b>'.$keywords['concurrency'].'</b>, Results: <b>'.$keywords['found_results'].'</b>, Search Volume: <b>'.$keywords['region_queries_count'].'</b>.</li>'.PHP_EOL;
echo '</ol>';
 }
}    

var_dump($obj) return:
array(4) { ["result"]=> array(16) { ["keyword"]=> string(3) "seo" ["cost"]=> float(0.54) ["concurrency"]=> int(25) ["found_results"]=> int(422000000) ["region_queries_count"]=> int(1900) ["region_queries_count_wide"]=> int(0) ["region_queries_count_phrase"]=> int(0) ["types"]=> array(2) { [0]=> string(14) "related_search" [1]=> string(3) "pic" } ["geo_names"]=> array(0) { } ["social_domains"]=> array(2) { [0]=> string(9) "wikipedia" [1]=> string(8) "linkedin" } ["right_spelling"]=> NULL ["keyword_length"]=> int(1) ["lang"]=> NULL ["keyword_id"]=> string(6) "647713" ["suggestions_count"]=> int(12528) ["keywords_count"]=> int(1637) } ["status_msg"]=> string(2) "OK" ["status_code"]=> int(200) ["left_lines"]=> int(99791) }

2

Answers


  1. You are iterating through the $obj['results'] array, but that is already the root element you want for your keywords. Just use this instead:

    $keywords = $obj['result'];
    echo '<li>Keyword: "'.$keywords['keyword'].'", Adwords CPC: <b>'.$keywords['cost'].'</b> USD, Concurrency: <b>'.$keywords['concurrency'].'</b>, Results: <b>'.$keywords['found_results'].'</b>, Search Volume: <b>'.$keywords['region_queries_count'].'</b>.</li>'.PHP_EOL;
    echo '</ol>';
    

    Output:

    <li>Keyword: "seo", Adwords CPC: <b>0.54</b> USD, Concurrency: <b>25</b>, Results: <b>422000000</b>, Search Volume: <b>1900</b>.</li> </ol>
    

    Demo on 3v4l.org

    Login or Signup to reply.
  2. If there is the possibility of having multiple keywords, then this could be presented as an array of objects. Whereas a single keyword is presented by a standalone JSON object. To cope with both of these, this code will check if it is an array of objects, if it isn’t then it converts the single entry into an array so that both options can be processed using the same code…

    $obj = json_decode($json, true);
    if (isset($obj['result'])) {
        // If not a numerically indexed array - convert it
        if ( !isset($obj['result'][0]))  {
            $obj['result'] = [$obj['result']];
        }
        foreach ($obj['result'] as $keywords) {
            echo '<li>Keyword: "'.$keywords['keyword'].'", Adwords CPC: <b>'.$keywords['cost'].'</b> USD, Concurrency: <b>'.$keywords['concurrency'].'</b>, Results: <b>'.$keywords['found_results'].'</b>, Search Volume: <b>'.$keywords['region_queries_count'].'</b>.</li>'.PHP_EOL;
            echo '</ol>';
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search