doukucai0099 2018-10-22 16:07
浏览 20

一次考虑一个数组元素

I am using webmerge to create documents.

I have a rule that looks like this (ignore the {$docType} conditional for this question):

enter image description here

And I pass JSON data that looks like this:

{   
    "forms": [
        {
      "formID": 19,
      "formUrl": "https://www.webmerge.me/merge/123456/y3wpgu",
      "fillIns": {
        "retainedAmount": "2000000"
        }
      },
      {
      "formID": 20,
      "formUrl": "https://www.webmerge.me/merge/111111/a2X5yn",
      "fillIns": []
      }
   ]
 }

This works great and will load each form based on the URL provided as needed.

However, if I have two identical forms with differing fillIns, like this:

{   
    "forms": [
        {
      "formID": 19,
      "formUrl": "https://www.webmerge.me/merge/123456/y3wpgu",
      "fillIns": {
        "retainedAmount": "1000000"
                }
        },
      {
      "formID": 19,
      "formUrl": "https://www.webmerge.me/merge/123456/y3wpgu",
      "fillIns": {
        "retainedAmount": "2000000"
        }
      }
   ]
 }

It merges two forms as it should, but each form has both values for retainedAmount, based on the following mapping:

{foreach from=$forms item=_row}{if $_row.formID == 19}{if !empty($_row.fillIns.retainedAmount)}{$_row.fillIns.retainedAmount|number_format}{/if}{/if}{/foreach} 

So the final result for both forms will look like this:

Retained Amount: 10000002000000

The result I want is that the first instance of the form displays

Retained Amount: 1000000

and the second displays

Retained Amount: 2000000

It appears that since the mapping is looping through each element in the array every time, that is why each form is adding both values.

I have been playing with array_shift and array_pop so that the rule will repeat but only consider one element at a time, but I cannot seem to get it to work correctly.

I think I need to get each element into a variable and then reference that variable in the map, but I cannot seem to get it to work. I would show all my attempts, but it will just be ugly, I think.

Lastly, I will never know at which index or how many identical forms with differing fillIns will appear, so I cannot use a key to guide me either.

Since I realize that not everyone has webmerge, a final result via print would be something like this:

FormID = 19 |Retained Amount = 1000000
FormID = 19 |Retained Amount = 2000000
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)