0

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

0

2 Answers 2

1

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 of Id: '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:

{% for status in app.session.get('statusArray') %}
    {% for dict in app.session.get('dictionaryArray') %}
        {% if status.Status == dict.Id %}
            {{ dict.Name }}
        {% endif %}
    {% endfor %}
{% endfor %}
Sign up to request clarification or add additional context in comments.

3 Comments

That's an awful lot of ifs. Maybe you can work around that.
@AdamArold Well you could make an associative array of dict Id to Name in the controller and use that instead, but then you'd be limited to that info. I don't know what else the OP might need.
Thank you :). In my controller i did json_decode the array's. I was trying to protect some company data so i may have screwed up the arryas :P's. Your solution worked however thank you very much.
0

I found this to work...

in php file:

$session = $this->get('session');
$session->set('user',array(
    'nickname' => 'Joe'
));

in twig template:

<p>{{ app.session.get('user')['nickname'] }}</p>

HOWEVER if the key does not exist, an exception is thrown.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.