获得第一Smarty的阵列的元素 [英] Get first Smarty Array-Element

查看:124
本文介绍了获得第一Smarty的阵列的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能拿到第一Smarty的数组元素?

其实我知道如何做到这一点......但我有以下问题。

我得到通过的阵列看起来像这样

  [数据] =>排列
        (
            [12] =>排列
                (
                    [内容] => FOOBAR
                )            [1] =>排列
                (
                    [内容] =>
                )            [2] =>排列
                (
                    [内容] =>
                )            [3] =>排列
                (
                    [内容] =>
                ) //这是一个snipit {$ myVar的| @print_r}

这下山,直到 [11]

由于某些原因没有 [0] [12] 在这个位置。

我不知道这是否会成为百达12,但我知道这是永诺在第一个位置。

因为具有相同的排序顺序另一个数组我无法排序这阵,我必须保持这个顺序后输出。

有没有办法选择的第一个元素,而无需使用数组[0] array.0 <? / p>

简介:项目IM工作的使用Smarty的2

修改


  

      
  • 我要重新索引数组,如果我知道如何:)

  •   

既然现在出现了几个小时后的answere,我即暂时解决了我的问题。

要解决这个问题,我打开 {PHP} 中的智者,得到了数组我需要的,分裂为两个数组(重新索引当然他们因此开始于 0 )。比我临时保存POS 0 从每个数组到临时数组。覆盖原稿 0 用下划线(_),比multisort他们,原值放回 0 ,并通过他们回 $这个 - &GT; _tpl_vars 很复杂的方式。 (所有的第三方物流内部)

  {} PHP
    //获取数组数据并重新建立索引
    $ I = 0;
    的foreach($这个 - &GT; _tpl_vars ['选项']为$选项)
    {
        的foreach($选项['数据']为$数据)
            $阵列[$ i] [] = $的数据;
        $ I ++;
    }    //删除空entrys
    $ I = 0;
    的foreach($数组[1] $数据){
        如果(修剪($数据['内容'])!=){
            $ S [] = $阵列[0] [$ i];
            $ A [] = $阵列[1] [$ i];
        }
        $ I ++;
    }    //临时保存第一值
    $ tmp_s = $ S [0];
    $ tmp_a = $ a [0];    //覆盖第一个值有一个干净的排序,并保持他们在POS 0值
    $ S [0] = $ A [0] =_;    //数组排序
    在array_multisort($ S,$ A);    //放回原来的值
    $ S [0] = $ tmp_s;
    $ A [0] = $ tmp_a;    //数组传递回tpl_vars
    $这个 - &GT; _tpl_vars ['new_options'] [] = $ S;
    $这个 - &GT; _tpl_vars ['new_options'] [] = $ A;
{/ PHP}


解决方案

如果在PHP中你有:

  $ smarty-&GT;分配(
    myVar的,
    阵列('数据'=&GT;
              阵列(
                  12 =&GT;阵列('内容'=&GT;第一要素),
                  1 =&GT;阵列('内容'=&GT;'第二个元素')
              ))
);

在Smarty的你可以使用:

  {VAR分配首先= =值} $myVar.DATA|@key
{$ myVar.DATA。$ first.content}

,你会得到显示:


  

第一个元素


不过,如果在PHP中使用:

  $数据=阵列(
    12 =&GT;阵列('内容'=&GT;第一要素),
    1 =&GT;阵列('内容'=&GT;'第二个元素')
);下($数据);$ smarty-&GT;分配(
    myVar的,
    阵列('数据'=&GT; $数据
              )
);

和在Smarty的有一样我发现之初,其结果将是:


  

第二个元素


您需要拨打:

 复位($的数据);

 下一个($的数据);

我认为你不能调用Smarty的重置数组,但我可能是错的。

它也可以在Smarty的重置数组,但它不是那么容易了。

如果在PHP中你有:

  $数据=阵列(
    12 =&GT;阵列('内容'=&GT;第一要素),
    1 =&GT;阵列('内容'=&GT;'第二个元素')
);下($数据);$ smarty-&GT;分配(
    myVar的,
    阵列('数据'=&GT; $数据
              )
);

在Smarty的你可以使用:

  {VAR分配= $ myVar.DATA value=$myVar.DATA|@reset}{VAR分配首先= =值} $myVar.DATA|@key
{$ myVar.DATA。$ first.content}

和您将获得:


  

第一个元素


作为结果

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}

解决方案

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

这篇关于获得第一Smarty的阵列的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆