Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sum organized array in php twig?

I want to sum array in twig which has similar name. I have tried following but stuck how to sum the array.

                {% for job in jobs %}
                    {% if not attribute(output, job.getJobName()) is defined %}
                        {% set output = output|merge({ (job.getJobName()) : {}, }) %}
                    {% endif %}                        
                    {% set output = output|merge({(job.getJobName()) : output[job.getJobName()] | merge([job,])}) %}
                {% endfor %}
                    {% for group_title, items in output  %}
                        <tr>
                            <td>{{ job_name }}</td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td>Total Salary</td>
                            <td>{{ total_salary }}</td> //how to get total salary
                        </tr>
                        {% for item in items %}
                            {% if (item) %}
                                <tr>
                                    <td>{{ item.getEmpName() }}</td>
                <td>{{ item.getEmpSalary() }}</td>
                                </tr>
                            {% endif %}
                        {% endfor %}                        
                    {% endfor %}

On Dump this are the arrays

array:21 [▼
  0 => Model\JobItem {#3330 ▶}
  1 => Model\JobItem {#3336 ▶}
  2 => Model\JobItem {#3341 ▶}
  3 => Model\JobItem {#3584 ▶}
  4 => Model\JobItem {#3320 ▶}
  5 => Model\JobItem {#3314 ▶}
  6 => Model\JobItem {#3309 ▶}
  7 => Model\JobItem {#3303 ▶}
  8 => Model\JobItem {#3298 ▶}
  9 => Model\JobItem {#3292 ▶}
  10 => Model\JobItem {#3288 ▶}
  11 => Model\JobItem {#2845 ▶}
  12 => Model\JobItem {#2728 ▶}
  13 => Model\JobItem {#2722 ▶}
  14 => Model\JobItem {#2996 ▶}
  15 => Model\JobItem {#3004 ▶}
  16 => Model\JobItem {#2746 ▶}
  17 => Model\JobItem {#2751 ▶}
  18 => Model\JobItem {#2756 ▶}
  19 => Model\JobItem {#2765 ▶}
  20 => Model\JobItem {#2842 ▶}
]
array:2 [▼
  0 => Model\JobItem {#2858 ▶}
  1 => Model\JobItem {#2863 ▶}
]

What I want to achieve here is how to get total salary from each array?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just loop the items another time and add all the salaries

{% for group_title, items in output  %}
    {% set total_salary = 0.00 %}
    {% for item in items %}
        {% set total_salary = total_salary + item.getEmpSalary() %}
    {% endfor %}

    <tr>
        <td>{{ job_name }}</td>
        <td></td>
        <td></td>
        <td></td>
        <td>Total Salary</td>
        <td>{{ total_salary }}</td> //how to get total salary
    </tr>
    {% for item in items %}
        {% if (item) %}
            <tr>
                <td>{{ item.getEmpName() }}</td>
                <td>{{ item.getEmpSalary() }}</td>
            </tr>
        {% endif %}
    {% endfor %}                        
{% endfor %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading