Skip to content

JSON to multidimensional array and looping query #1

Description

@coxy17

I have the following test JSON data returned from an API
{ "products": [ { "ID": 271, "post_author": "1", "post_date": "2022-01-07 13:25:07", "post_date_gmt": "2022-01-07 13:25:07", "post_content": "test ", "post_title": "Test Product 2", "post_type": "product", "post_mime_type": "", "comment_count": "0", "filter": "raw", "product_type": "Gift", "product_start_date": "2022-01-14 00:00:00", "product_end_date": "2022-01-22 00:00:00", "product_price": "12.00", "product_condition": "new", "product_location": "Nottingham", "product_images": [ "products/271/ygm_primary_logo.png" ] }, { "ID": 270, "post_author": "1", "post_date": "2022-01-05 22:55:27", "post_date_gmt": "2022-01-05 22:55:27", "post_content": "test", "post_title": "Test Product 1", "product_type": "Gift", "product_start_date": "2022-01-06 00:00:00", "product_end_date": "2022-01-15 00:00:00", "product_price": "22.00", "product_condition": "new", "product_location": "Hucknall", "product_images": [ "products/270/ygm_yellowface_click.png", "products/270/ygm_logo_small.png", "products/270/ygm_reversed_logo.png", "products/270/ygm_flyer_dec_front_sold_out.png" ] } ] }

I then decode it in PHP (output below) and pass it into Jellyfish like so
$page->set("products", json_decode($json, true));

array (size=1) 'products' => array (size=2) 0 => array (size=31) 'ID' => int 271 'post_author' => string '1' (length=1) 'post_date' => string '2022-01-07 13:25:07' (length=19) 'post_date_gmt' => string '2022-01-07 13:25:07' (length=19) 'post_content' => string 'hijhjkhbkj hj ' (length=14) 'post_title' => string 'Test Product New' (length=16) 'product_type' => string 'Gift' (length=4) 'product_start_date' => string '2022-01-14 00:00:00' (length=19) 'product_end_date' => string '2022-01-22 00:00:00' (length=19) 'product_price' => string '12.00' (length=5) 'product_condition' => string 'new' (length=3) 'product_location' => string 'Nottingham' (length=10) 'product_images' => array (size=1) ...

But in my template file I am not sure how to iterate through and call? any help would be great. The below code just returns 'Array'

{% for key=>item in {{ products }} %} <b>{{key}}->{{item}}</b> {%endfor%}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions