Merging arrays in a template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • unixman
    Senior Member
    • Jun 2001
    • 220
    • 5.2.x

    Merging arrays in a template

    Quick templating question. In the template markup below, I create a new array called "testArr", and attempt to populate it by calling array_merge. The first debug statement shows a properly initiatlized array with 0 elements in it. The second debug statement shows that "fields" is populated during each iteration of the loop. But for some reason, the debug statement at the end shows a NULL value for the final array. Any ideas? Thanks in advance!

    Code:
        {vb:set testArr, {vb:php array}}
        {vb:debugvardump testArr}
    
        <vb:each from="userInfo.customFields" key="category" value="fields">
            {vb:debugvardump fields}
            <vb:each from="fields" key="name" value="field">
              <vb:if condition="$category != 'default'" >
                  {vb:set testArr, {vb:php array_merge, testArr, field}}
              </vb:if>
            </vb:if>
        </vb:each>
    
        {vb:debugvardump testArr}
    Also, the docs mention that you can use "array()" with "vb: php". I had to remove the parentheses to get it to work at all. FYI!

    Cheers.
    Last edited by unixman; Mon 28 Nov '16, 11:06am.
    Incursus: (latin: clash, collision/attack, raid, foray, invasion)
    We Create Thingz.
  • glennrocksvb
    Former vBulletin Developer
    • Mar 2011
    • 3955
    • 5.7.X

    #2
    The inner <vb:each> loop has wrong closing tag <vb:if>. I don't know if that's just a typo in your post.

    You're using array merge but $field variable is not an array, right? If it was, then the code should have been:

    Code:
    {vb:set testArr, {vb:php array_merge, {vb:raw testArr}, {vb:raw field}}}
    Either way, you don't need array_merge. You could simply do this to push $field to the $testArr array.

    Code:
    {vb:set testArr[], {vb:raw field}}
    Also make sure that your if-condition for checking $category evaluates to true.

    Flag Icon Postbit Insert GIPHY Impersonate User INITIALS AVATAR Better Name Card Quote Selected Text Collapsed PM Post Footer Translate Stop Links in Posts +MORE!

    Comment

    • unixman
      Senior Member
      • Jun 2001
      • 220
      • 5.2.x

      #3
      Thanks for the response! Yes, that vb: if is actually an else. Was a typo in the post. Will try your suggestions - thanks!
      Incursus: (latin: clash, collision/attack, raid, foray, invasion)
      We Create Thingz.

      Comment

      • unixman
        Senior Member
        • Jun 2001
        • 220
        • 5.2.x

        #4
        yep - array_push did the trick!
        Incursus: (latin: clash, collision/attack, raid, foray, invasion)
        We Create Thingz.

        Comment

        Related Topics

        Collapse

        Working...
        😀
        😂
        🥰
        😘
        🤢
        😎
        😞
        😡
        👍
        👎