3

How can i use array_push from PHP functions in Smarty Template.

I tried this

{assign var='out' value=array()}
{foreach $data['data'] as $dataInfo}
   {$out|@array_push {$dataInfo['a']}:{$dataInfo['b']}}

{/foreach}

{$out|var_dump}
3
  • 1
    Why? Surely you should be doing this in the model, or perhaps the controller, but not the view Commented Sep 10, 2014 at 9:50
  • I want the view decide how to display not the controller, but this is not my problem. I really want to know how to do this Commented Sep 10, 2014 at 9:53
  • 2
    Here you're not deciding how to display, but generate data - usually what the model layer is supposed to do. Commented Sep 10, 2014 at 9:55

1 Answer 1

9

You haven't explained what result you want to achieve and in fact you should rather do such things in controller/model than in view.

However if in PHP you have:

$smarty->assign(
    'data',
    array(
        'data' => array(
            array('a' => 'one', 'b' => 'two'),
            array('a' => 'three', 'b' => 'four')
        )
    )
);

And in Smarty file you have:

{assign var='out' value=array()}
{foreach $data['data'] as $dataInfo}

    {append var='out' value=$dataInfo['a']}
    {append var='out' value=$dataInfo['b']}

{/foreach}

{$out|var_dump}

Output will be:

array(4) { [0]=> string(3) "one" [1]=> string(3) "two" [2]=> string(5) "three" [3]=> string(4) "four" } 

as expected.

array_push in this case is not the best solution because it will also display number of elements, so using:

{assign var='out' value=array()}
{foreach $data['data'] as $dataInfo}
    {$out|array_push:$dataInfo['a']}<br />
    {$out|array_push:$dataInfo['b']}<br />

{/foreach}

{$out|var_dump}

you would also get numbers displayed:

1
2
3
4
array(4) { [0]=> string(3) "one" [1]=> string(3) "two" [2]=> string(5) "three" [3]=> string(4) "four" } 

EDITED ANSWER ACCORDING TO COMMENT

I want to display like this: a:b, c:d where this elements are| $array[0] = Array('aa' => 'a', 'bb' => 'b') $array[1] = Array('cc'=>'c', 'dd'=>'d');

If in PHP you have:

$array = array();
$array[0] = Array('aa' => 'a', 'bb' => 'b');
$array[1] = Array('cc'=>'c', 'dd'=>'d');

$smarty->assign(
    'data', $array
    );

In Smarty you should use:

{foreach $data as $dataInfo}
    {$dataInfo|implode:':'}{if not $dataInfo@last}, {/if}
{/foreach}

Output will be:

a:b, c:d 

But it's not connected to the question in anyway where you asked about using PHP function array_push in Smarty template

Sign up to request clarification or add additional context in comments.

2 Comments

I want to display like this: a:b, c:d where this elements are| $array[0] = Array('aa' => 'a', 'bb' => 'b') $array[1] = Array('cc'=>'c', 'dd'=>'d');
@OsomA What do you mean? You told that you want to put data into array. And var_dump function displays data only in its format. If you wanted simple display data why you wanted to use array_push? I don't get it. You should edit your question and explain what you want to achieve, put there original PHP data (in var_export and not var_dump format)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.