Make a new multi dimensional array from the existing array in smarty

114 views Asked by At

My whole moto in this question is to make a new array from existing array and to reverse the first and second index of array.
I have an array

 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] => 
                    )

            )

    )

)

Now i have to make a new array with first index on second and second on first index like this But have to do only in smarty not in php etc

   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

There are 1 answers

0
Ninke On BEST ANSWER

I think the following might help you.

You could loop the array you have and fill a new array with the values. Lets call the original $firstArray and the new one $newArray. I see that the first item from the subarrays are what you need(and there are no other items apart from 0) so you could do the following:

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

$item.0 selects the first subarray(there are no other). You might have to use $item[0] depending on your smarty version. If there is more than 1 subarray, then loop $items first before you add an array to the $newArray.

It looks as if there's another array encapsuling your firstArray, so perhaps you need to select the subarray from the main one first....

note: I used smarty 3. Perhaps you use a different version, be sure to check the syntax for your version.