The |push
filter is applied to a tag returning a PHP array/table (seer #ARRAY) and adds a new value to it.
The key is incremented by 1 for each new value. if no key has been defined for the first value, the table is indexed starting from 0 (the first key is 0).
#SET{an_array,#ARRAY{5,one_value}}
#SET{an_array,#GET{an_array}|push{another_value}}
#SET{an_array,#GET{an_array}|push{a_third_value}}
[(#GET{an_array}|foreach)]
displays:
- 5=> one_value
- 6=> another_value
- 7=> a_third_value
The first key is 5 because it was specified during the creation of the table (the initial #SET
command).
A common usage example of |push
:
#SET{my_articles, #ARRAY}
<BOUCLE_my_articles(ARTICLES){...}>
#SET{my_articles, #GET{my_articles}|push{#ID_ARTICLE}}
</BOUCLE_my_articles>
If the articles selected by the (BOUCLE) LOOP bear the numbers 2, 5 and 10, [(#GET{my_articles}|foreach)]
will return:
- 0=>2
- 1=>5
- 2=>10
In this case, the first key is zero since there was no key defined beforehand.
In contrast to the PHP function array_push
, the SPIP |push
filter does not generate an error message if the filtered tag is not a table.