Announcement

Collapse
No announcement yet.

Merging arrays in a template

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 10:06am.
    Incursus: (latin: clash, collision/attack, raid, foray, invasion)
    We Create Thingz.

  • #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.

    GIPHY for vB5 AutoLinker Auto-Create Flag Report Topic Social Icons in Postbit Clear Cache Cron DragDrop Upload Topic AJAX AutoUpdate Custom Avatars Selector Stop Links in Posts...and more!

    Comment


    • #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


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

        Comment

        Related Topics

        Collapse

        Working...
        X