Get first Smarty Array-Element
Asked Answered
E

2

6

How can i get the first smarty array element ?

Actually i know how to do this... but i have following issue.

I get a array passed that looks like this

[DATA] => Array
        (
            [12] => Array
                (
                    [content] => foobar
                )

            [1] => Array
                (
                    [content] =>  
                )

            [2] => Array
                (
                    [content] =>  
                )

            [3] => Array
                (
                    [content] =>  
                )

 //this is a snipit of {$myVar|@print_r}

this goes down until [11]

For some reason there is no [0] and a [12] at this position.

I don't know if this will allways be 12 but I know it allways be at the first position.

I can't sort this array because there is another array that has the same sortorder and I have to keep this order for later output.

Is there a way to select the first element without using array[0] or array.0 ?

Info: The project im working on uses Smarty 2

EDIT

  • I would re-index the array if i knew how :)

Since there is now answere after a couple of hours, i solved my problem temporarly.

To solve this, I opened {php} in smarty, got the array I need, splitted it into two arrays (re-indexing them of course so it starts at 0). Than I temp-save pos 0 from each array to a temp array. Override the orig 0 with underscore (_) and than multisort them, put back the original value to 0 and pass them back to $this->_tpl_vars very complicated way. (all inside the tpl)

{php}
    // get array data and re-index it
    $i=0;
    foreach( $this->_tpl_vars['options'] as $option )
    {
        foreach( $option['DATA'] as $data )
            $array[$i][] = $data;
        $i++;
    }

    //delete empty entrys
    $i=0;
    foreach( $array[1] as $data ){
        if(trim($data['content']) != ""){
            $s[] = $array[0][$i];
            $a[] = $array[1][$i];
        }
        $i++;
    }

    //temp save first values
    $tmp_s  = $s[0];
    $tmp_a  = $a[0];

    //override first values to have a clean sort and keep them values on pos 0
    $s[0] = $a[0] = "_";

    //sort the arrays
    array_multisort($s,$a);

    //putting back the original values
    $s[0] = $tmp_s;
    $a[0] = $tmp_a;

    //pass the array back to tpl_vars
    $this->_tpl_vars['new_options'][] = $s;
    $this->_tpl_vars['new_options'][] = $a;


{/php}
Edmonds answered 22/8, 2014 at 14:0 Comment(0)
L
22

IF in PHP you have:

$smarty->assign(
    'myVar',
    array('DATA' =>
              array(
                  12 => array('content' => 'first element'),
                  1 => array('content' => 'second element')
              ))
);

In Smarty you can use:

{assign var=first value = $myVar.DATA|@key}
{$myVar.DATA.$first.content}

And you will get displayed:

first element

However if in PHP you use:

$data = array(
    12 => array('content' => 'first element'),
    1 => array('content' => 'second element')
);

next($data);

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

And in Smarty have the same as I showed at the beginning, the result will be:

second element

You would need to call:

reset($data);

after

next($data);

I think you cannot call reset for array in Smarty but I could be wrong.

It's also possible to reset array in Smarty but it's not so easy.

If in PHP you have:

$data = array(
    12 => array('content' => 'first element'),
    1 => array('content' => 'second element')
);

next($data);

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

In Smarty you could use:

{assign var=$myVar.DATA value=$myVar.DATA|@reset}

{assign var=first value = $myVar.DATA|@key}
{$myVar.DATA.$first.content}

And you will get:

first element

as result

Lingo answered 22/8, 2014 at 19:13 Comment(0)
G
2

If your data is in array {$data} then using Smarty 3 you can just

{$firstData = $data|reset}
Gutshall answered 14/4, 2020 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.