在smarty中从现有阵列创建一个新的多维数组

时间:2015-06-11 06:41:43

标签: smarty

我在这个问题中的全部动机是从现有数组中创建一个新数组并反转数组的第一个和第二个索引。
我有一个数组

 Array
(
[0] => Array
    (
        [0] => Array
            (
                [field] => Array
                    (
                        [name] => name
                        [tabindex] => 0
                    )

                [colspan] => 3
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [field] => Array
                    (
                        [name] => sequence
                        [tabindex] => 0
                    )

                [colspan] => 3
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [field] => Array
                    (
                        [name] => description
                        [tabindex] => 0
                    )

                [colspan] => 3
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [field] => Array
                    (
                        [name] => status
                        [tabindex] => 0
                    )

                [colspan] => 3
            )

    )

[4] => Array
    (
        [0] => Array
            (
                [field] => Array
                    (
                        [name] => modified_by_name
                        [customCode] => {$fields.date_modified.value} {$APP.LBL_BY} {$fields.modified_by_name.value}
                        [label] => LBL_DATE_MODIFIED
                        [tabindex] => 0
                    )

                [colspan] => 3
            )

    )

[5] => Array
    (
        [0] => Array
            (
                [field] => Array
                    (
                        [name] => created_by_name
                        [customCode] => {$fields.date_entered.value} {$APP.LBL_BY} {$fields.created_by_name.value}
                        [label] => LBL_DATE_ENTERED
                        [tabindex] => 0
                    )

                [colspan] => 3
            )

    )

[6] => Array
    (
        [0] => Array
            (
                [field] => Array
                    (
                        [name] => 
                    )

            )

        [1] => Array
            (
                [field] => Array
                    (
                        [name] => 
                    )

            )

    )

)

现在我必须创建一个新的数组,第一个索引在第二个和第二个第一个索引像这样但是只能在smarty而不是在php等

   Array
   (
[0] => Array
    (
        [0] => Array
            (
                [field] => Array
                    (
                        [name] => name
                        [tabindex] => 0
                    )

                [colspan] => 3
            )

        [1] => Array
            (
                [field] => Array
                    (
                        [name] => sequence
                        [tabindex] => 0
                    )

                [colspan] => 3
            )

        [2] => Array
            (
                [field] => Array
                    (
                        [name] => description
                        [tabindex] => 0
                    )

                [colspan] => 3
            )
   [3] => Array
            (
                [field] => Array
                    (
                        [name] => status
                        [tabindex] => 0
                    )

                [colspan] => 3
            )
  [4] => Array
            (
                [field] => Array
                    (
                        [name] => modified_by_name
                        [customCode] => {$fields.date_modified.value} {$APP.LBL_BY} {$fields.modified_by_name.value}
                        [label] => LBL_DATE_MODIFIED
                        [tabindex] => 0
                    )

                [colspan] => 3
            )
 [5] => Array
            (
                [field] => Array
                    (
                        [name] => created_by_name
                        [customCode] => {$fields.date_entered.value} {$APP.LBL_BY} {$fields.created_by_name.value}
                        [label] => LBL_DATE_ENTERED
                        [tabindex] => 0
                    )

                [colspan] => 3
            )

    )
)

1 个答案:

答案 0 :(得分:1)

我认为以下内容对您有帮助。

您可以循环您拥有的数组并使用值填充新数组。让我们调用原始的$ firstArray和新的$ newArray。我看到子阵列中的第一项是您需要的(除了0之外没有其他项目),所以您可以执行以下操作:

{$newArray = []}
{foreach $firstArray as $item}
    {$newArray[] = $item.0}
{/foreach}

$ item.0选择第一个子阵列(没有其他子阵列)。您可能必须使用$ item [0],具体取决于您的智能版本。 如果有超过1个子数组,则在将数组添加到$ newArray之前先循环$ items。

看起来好像还有另一个数组封装了你的firstArray,所以也许你需要先从主要数据库中选择子数组......

注意:我使用的是smarty 3.也许您使用的是其他版本,请务必检查您的版本的语法。