1

Below is my category array containing sub arrays and I want to find length of sub-array of each category array item.

[category] => Array(
[0] => Array(
    [name] => Desktops
    [children] => Array(
        [0] => Array(
            [name] => PC
            [children3] => Array()
            [column] => 1
            [href] => http://192.168.0.101/user1/opc_laddylike3.0/upload/desktops/pc
            )
        [1] => Array(
            [name] => Mac
            [children3] => Array()
            [column] => 1
            [href] => http://192.168.0.101/user1/opc_laddylike3.0/upload/desktops/mac
            )
        )
    [column] => 1
    [href] => http://192.168.0.101/user1/opc_laddylike3.0/upload/desktops
    )
[1] => Array(
    [name] => Laptops & Notebooks
    [children] => Array(
        [0] => Array(
            [name] => Macs
            [children3] => Array()
            [column] => 1
            [href] => http://192.168.0.101/user1/opc_laddylike3.0/upload/laptop-notebook/macs
            )
        [1] => Array(
            [name] => Windows
            [children3] => Array()
            [column] => 1
            [href] => http://192.168.0.101/user1/opc_laddylike3.0/upload/laptop-notebook/windows
            )
        )
   [column] => 1
   [href] => http://192.168.0.101/user1/opc_laddylike3.0/upload/laptop-notebook
 )

)

I want to find length of children of category array. Please check my code and help if I am wrong.

{% for category in categories %} 
{% if category.children %}
{% for n in 0..(category.children|length) %} 

Here I am getting category.children length as 1 for all array items.

Thanks in advance

Radhika
  • 533
  • 5
  • 22
  • Did you try storing category.children|length in a different variable and using it? {% set x = (category.children|length) %} {% for n in 0..x %} – Aleem Mar 21 '18 at 08:00
  • Thank you. Yes,I did. But still not working – Radhika Mar 21 '18 at 08:03
  • I tried copying the same array, I get the length as 3 {% for category in categories %} {% if category.children %} {% set count=1 %} {% for n in 0..(category.children|length) %} {{ n }} {% endfor %} {% endif %} {% endfor %} This prints 0 1 2 0 1 2 for me – Aleem Mar 21 '18 at 08:14
  • [2] => Array ( [name] => Components [children] => Array ( [0] => Array ( [name] => balls [children3] => Array ( ) [column] => 2 [href] => mouse ) [1] => Array ( [name] => Monitors [children3] => Array ( [0] => Array ( [name] => test 1 [href] => monitor/test1 ) [1] => Array ( [name] => test 2 [href] => children3] => Array ( ) [column] => 2 [href] => printer ) [3] => Array ( [name] => Scanners [children3] => Array ( ) [column] => 2 [href] => scanner ) [4] => Array ( [name] => Web Cameras [children3] => Array ( ) [column] => 2 [href] => ) ) [column] => 2 [href] => ) – Radhika Mar 21 '18 at 08:43
  • check this https://stackoverflow.com/questions/28671386/how-to-check-a-multidimensional-twig-array-for-values – gkd Mar 21 '18 at 09:04
  • Please use above array to test – Radhika Mar 21 '18 at 09:07
  • 2
    i try in [this twigfiddle](https://twigfiddle.com/ub9mhg) without any problem... check the variable names – Matteo Mar 21 '18 at 20:59

0 Answers0