Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON to multidimensional array and looping query #1

Open
coxy17 opened this issue Jan 7, 2022 · 0 comments
Open

JSON to multidimensional array and looping query #1

coxy17 opened this issue Jan 7, 2022 · 0 comments

Comments

@coxy17
Copy link

coxy17 commented Jan 7, 2022

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%}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant