R
R
Roman Komarnitsky2018-07-09 14:24:02
1C-Bitrix
Roman Komarnitsky, 2018-07-09 14:24:02

How to fix array overwriting when component is called multiple times?

There is an array in the component template. When the component is called for the first time, it is filled with data, and everything works fine. But if the component is called again on the same page, the array is filled with new data, and the old ones are deleted. As a result, only the last call to the component works. How to treat this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Komarnitsky, 2018-07-10
@romkom

I figured it out myself, I'll describe the algorithm, maybe it will be useful to someone:
1. You need to create an empty array outside the component template, for example in header.php
2. Glue an empty array in the component template, and the array whose data needs to be saved. (I passed the php array to js, ​​so I used the concat function for this)
3. Next, using jquery, namely the $.extend() function, I glued the arrays that were written into an empty array when the components were called.
4. As a result, there is one whole array with all the data that the component passed when iterating its calls.

M
Maxim, 2018-07-09
@Tomio

Try storing the array in $GLOBALS or $_SESSION.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question