[ar] [ast] [bg] [br] [ca] [co] [cpf] [cs] [da] [de] [en] [eo] [es] [eu] [fa] [fon] [fr] [gl] [id] [it] [ja] [lb] [nl] [oc] [pl] [pt] [ro] [ru] [sk] [sv] [tr] [uk] [vi] [zh] Espace de traduction



June 2010

All the versions of this article:

|push is used to add a value to the end of an array/table.

The |push filter (since SPIP 2.0) 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).

[(#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:

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<code>->http://ch.php.net/manual/en/function.array-push.php], the SPIP <code>|push filter does not generate an error message if the filtered tag is not a table.

Show the template of this page Site powered by SPIP | Translation area | Private area