How to parse out variables from a JSON var_dump in a Wordpress Plugin

后端 未结 1 1536
醉话见心
醉话见心 2020-12-22 10:05

I\'m trying to pull in the JSON data from my API, and get the data out of each key to display nicely in HTML/CSS. Right now having trouble getting the data.

This is

1条回答
  •  暖寄归人
    2020-12-22 10:23

    This section of code is quite strange - what is it intended to do?

    $data = add_shortcode (
        array(
            'name' => 'name',
            'overview' => 'overview',
            'benefits' => 'benefits'
        ), $data
    );
    
    extract($data);
    

    To access your data you can simply do

    $data[0]['productname']
    

    You also have your output variables inside a string (inside ''). To bring your data in to the output try:

    $data = (json_decode($result, true));
    var_dump($data);
    $product = $data[0];
    $content = '';
    $content .='

    ' . $product['productname'] . '

    ' . $product['overview'] . '

    • ' . $product["benefits"][0] . '
    • ' . $product["benefits"][1] . '
    ';

    Or with a loop:

    $data = (json_decode($result, true));
    $content = '';
    foreach($data as $product) {
        $content .='

    ' . $product['productname'] . '

    ' . $product['overview'] . '

      '; foreach($product['benefits'] as $benefit) { $content .= '
    • ' . $benefit . '
    • '; } $content .= '
    '; }

    0 讨论(0)
提交回复
热议问题