I’ve got 2 separate array’s in separate session variables full of other arrays. I’m trying to pull out one attribute of an array based on the value output by another. It’s essentially status codes in one (with other attributes) and a dictionary in the others.
They look like this :
Dictionary array
%array% [ { Id: '22', Name: 'Foo', Type: 'FooFoo', Description: 'More Foo', Enabled: 'true', Priority: 'number here' },
{ Id: '23', Name: 'Bar', Type: BarBar, Description: 'oh look more bars', Enabled: 'true', Priority: 'number here' },{...}]
and
Status array:
%array% [{ Id: '54', Name: 'Name goes here', Status: '23', BrandName: 'Brand'}],{...} }]
What i’m trying to do is something like
{%for Id in app.session.get('statusArray')%}
{% if Id.Status in app.session.get('dictionaryArray')%}
{{app.session.get('dictionaryArray').Name}}
{% endif %}
{%endfor%}
I’ve also tried
{{attribute(app.session.get('dictionaryArray').Name, Id.Status)}}
Or something to that effect.
TL;DR
I need to pull out the Name from the Dictionary Array based upon the ‘Status: ‘ given by the Status Array
Okay so you have lots of issues in this code. Let’s start with the data.
The data you have provided seem to be in the form of JSON arrays, not PHP arrays. As such, you have to decode them first using
json_decode.But those arrays are not valid JSON. To make them valid JSON, each of the keys and values need to have double quotes around them, i.e.
"Id": "54"instead ofId: '54'.Then, you have to set those variables into the session using
$this->getRequest()->getSession()->set('statusArray', $statusArray);, which I assume you’re doing fine.Next, your logic for your twig templates is incorrect. Since you have multiple associative arrays within an outer array, each item in your for loop is one of those associative arrays that corresponds with the JSON object enclosed in braces {}.
Code that will get done what you want is as follows: