Set structured data values into variable values for parsing php

We have to parse solution where we use something like this.

foreach ($items as $item){
$title = $item[‘food’][‘title’];
$description = $item[‘food’][‘description’];
}

The thing is, we want to pull in the value in a loop. from structured data. (below is a simple example)

$structured_data_value = [‘food’][‘description’]

$title = $item . $structured _data_value;

// What we want to do is set this up with structured data so we can make call to any of these fields in a loop.

for instance. Structured data field would store [‘food’][‘title’] or [‘food’][‘description’].

Then instead of these being hard coded I can just say.

I read this twice and I still have no idea what you’re trying to achieve.
You want to use a structured field to store an array?

You can loop through a structured data, you can find how to do it in the documentation and you can access each value using native Kirby functions.

I have a hard time understanding why you need an array.

Can you post some code or at least show what is the final result you want to get?

You could for example have a structure like this in your blueprint:

fields:
  food:
    label : Food
    type  : structure
    fields:
      title:
        label : Title
        type  : text
      description:
        label : Description
        type  : textarea

And then loop through each entry like this…

<?php
foreach($page->food()->toStructure() as $item) :  
    $structured_data_value = $item->description();
endforeach;
?>

I’m also not sure, if I got it right, but I think you want to use flexible array elements instead of hardcoded ones!?

I think this won’t work …

But you can use a variable for one key of an array element like this …

$a = 'food';
$b = 'description';
$structured_data_value = $item[$a][$b];

Is this helpful for you, @bbarclay?

this is the idea, maybe I can do a switch statement have them use tags and determine tag 1 = $a tag 2 = $b tag 3 = $c then have them set the depth of the array. That way I can determine to use [$a][$b][$c] or [$a][$b] or [$a]. Something like that.

basically I’m trying to create a system that will allow me to drop in an end point. Set the values I want to extract such as [‘cat’][‘title’] or [‘dog’][‘title’] but in the panel. That way if I have another endpoint. All I have to do is specifiy the data I want out. That endpoint might be different like [car][make] or [car][year]. if it’s hard coded then it makes it tough for users who don’t have access to the code. This way they can read down the json file. Extract out the data by setting the parse up in the structured field. But it’s tricky because I don’t know how to send that array key after the variable item.

This still sounds confusing to me. I still don’t understand if we are talking about data from a Panel structure field (but these cannot be nested like your first examples suggest) that you want to parse in a template. Or if you.want to build a nested array from some sort of data. Or if you want to set up dynamic fields in your Panel forms.

When parsing json you can do the following.

Tags would be stored in structured data like this. [‘cat’][‘title’]

$resp = json_decode($getResponse, true);

Then I can do something like this.

foreach($resp as $item) {

echo $item[‘cat’][‘title’]
echo $item[‘cat’][‘description’]
echo $item['cat][‘id’]

}

What I’m looking to do is a structured table that I can set these values in [‘cat’][‘description’], [‘cat’][‘description’], ['cat][‘id’], etc.

This way, I can drop the json in and setup, from within the panel, the values that are to be parsed.

$events = $page->cats()->toStructure();

foreach($resp as $item) {

foreach ($events as event){

echo $item . $event->tag();

}

}

To recap what I think I understood:

  1. You get some JSON from somewhere that you want to parse in your template.
  2. Instead of parsing the complete JSON source, in the Panel, the editor should be able to define what information should be extracted from that JSON file.

But to make this work, the editor would have to know what to choose from. So you would have to fill some select fields with the possible options in each level, and these options should also be dynamic depending on what the user has already chosen?