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

Make a new multi dimensional array from the existing array in 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
            )

    )
)

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

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

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

$item.0 selects 第一个子数组(没有其他)。根据您的 smarty 版本,您可能必须使用 $item[0]。 如果有超过 1 个子数组,则在将数组添加到 $newArray 之前先循环 $items。

看起来好像有另一个数组封装了您的第一个数组,所以也许您需要先 select 主数组中的子数组....

注意:我使用的是 smarty 3。也许你使用的是不同的版本,一定要检查你的版本的语法。