获得第一Smarty的阵列的元素 [英] Get first Smarty Array-Element
问题描述
我怎样才能拿到第一Smarty的数组元素?
其实我知道如何做到这一点......但我有以下问题。
我得到通过的阵列看起来像这样
[数据] =>排列
(
[12] =>排列
(
[内容] => FOOBAR
) [1] =>排列
(
[内容] =>
) [2] =>排列
(
[内容] =>
) [3] =>排列
(
[内容] =>
) //这是一个snipit {$ myVar的| @print_r}
这下山,直到 [11]
由于某些原因没有 [0]
和 [12]
在这个位置。
我不知道这是否会成为百达12,但我知道这是永诺在第一个位置。
因为具有相同的排序顺序另一个数组我无法排序这阵,我必须保持这个顺序后输出。
有没有办法选择的第一个元素,而无需使用数组[0]
或 array.0
<? / p>
简介:项目IM工作的使用Smarty的2
修改
- 我要重新索引数组,如果我知道如何:)
块引用>既然现在出现了几个小时后的answere,我即暂时解决了我的问题。
要解决这个问题,我打开
{PHP}
中的智者,得到了数组我需要的,分裂为两个数组(重新索引当然他们因此开始于0
)。比我临时保存POS0
从每个数组到临时数组。覆盖原稿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]
orarray.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 at0
). Than I temp-save pos0
from each array to a temp array. Override the orig0
with underscore (_) and than multisort them, put back the original value to0
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屋!