我正在尝试在smarty模板引擎中使用foreach循环,以便我可以将下面给出的键值数组转换为所选语言。好吧,但我在核心PHP解决了这个问题。但是我在智能模板引擎中遇到了问题。
示例在php中
while($row = mysql_fetch_array($query)) {
$trans[$row['form_field_labels_key']] = $row['form_field_labels_name'];
}
我的键值在key = form_field_labels_name
和value =form_field_labels_name
。
给定数组:
Array
(
[0] => Array
(
[form_field_labels_id] => 3
[form_field_labels_key] => Personal_Information
[form_field_labels_name] => தனிப்பட்ட தகவல்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:21:27
[last_modified_date] =>
)
[1] => Array
(
[form_field_labels_id] => 6
[form_field_labels_key] => NRIC
[form_field_labels_name] => NRIC / இறகு / இல்லை அடையாள எண்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:23:13
[last_modified_date] =>
)
[2] => Array
(
[form_field_labels_id] => 9
[form_field_labels_key] => Title
[form_field_labels_name] => தலைப்பு
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:26:55
[last_modified_date] =>
)
[3] => Array
(
[form_field_labels_id] => 11
[form_field_labels_key] => Name
[form_field_labels_name] => பெயர்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:28:41
[last_modified_date] =>
)
[4] => Array
(
[form_field_labels_id] => 15
[form_field_labels_key] => Apartment_no
[form_field_labels_name] => அபார்ட்மெண்ட்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:30:34
[last_modified_date] =>
)
[5] => Array
(
[form_field_labels_id] => 18
[form_field_labels_key] => Apartment_name
[form_field_labels_name] => அடுக்குமாடி / கட்டிடம் பெயர்.
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:32:52
[last_modified_date] =>
)
[6] => Array
(
[form_field_labels_id] => 21
[form_field_labels_key] => Street_Name
[form_field_labels_name] => தெரு பெயர்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:35:40
[last_modified_date] =>
)
[7] => Array
(
[form_field_labels_id] => 24
[form_field_labels_key] => Unit_No
[form_field_labels_name] => அலகு இல்லை
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:37:25
[last_modified_date] =>
)
[8] => Array
(
[form_field_labels_id] => 27
[form_field_labels_key] => Province
[form_field_labels_name] => மாகாணம்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:39:38
[last_modified_date] =>
)
[9] => Array
(
[form_field_labels_id] => 30
[form_field_labels_key] => City_State
[form_field_labels_name] => நகரம் / மாநிலம்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:42:03
[last_modified_date] =>
)
[10] => Array
(
[form_field_labels_id] => 33
[form_field_labels_key] => Zip_Postal
[form_field_labels_name] => ஜிப் / அஞ்சல் குறியீடு
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:43:40
[last_modified_date] =>
)
[11] => Array
(
[form_field_labels_id] => 36
[form_field_labels_key] => Country
[form_field_labels_name] => நாடு
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:47:27
[last_modified_date] =>
)
[12] => Array
(
[form_field_labels_id] => 39
[form_field_labels_key] => Home_Tel
[form_field_labels_name] => வீட்டில் தொலைபேசி
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:48:42
[last_modified_date] =>
)
[13] => Array
(
[form_field_labels_id] => 42
[form_field_labels_key] => Office_Tel
[form_field_labels_name] => அலுவலக தொலைபேசி
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:50:25
[last_modified_date] =>
)
[14] => Array
(
[form_field_labels_id] => 45
[form_field_labels_key] => Mobile
[form_field_labels_name] => மொபைல்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:55:40
[last_modified_date] =>
)
[15] => Array
(
[form_field_labels_id] => 48
[form_field_labels_key] => Email
[form_field_labels_name] => மின்னஞ்சல்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:55:50
[last_modified_date] =>
)
[16] => Array
(
[form_field_labels_id] => 51
[form_field_labels_key] => Please_provide_email_for_successful
[form_field_labels_name] => வெற்றிகரமான ஆன்லைன் உத்தரவாதத்தை பதிவு அறிவிப்பு மின்னஞ்சல் வழங்கவும்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 15:58:45
[last_modified_date] =>
)
[17] => Array
(
[form_field_labels_id] => 54
[form_field_labels_key] => Purchase_Information
[form_field_labels_name] => தகவல் வாங்க
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:08:04
[last_modified_date] =>
)
[18] => Array
(
[form_field_labels_id] => 57
[form_field_labels_key] => Receipt_No
[form_field_labels_name] => ரசீது இல்லை
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:09:16
[last_modified_date] =>
)
[19] => Array
(
[form_field_labels_id] => 60
[form_field_labels_key] => Date_of_Purchase
[form_field_labels_name] => கொள்முதல் தேதி
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:11:51
[last_modified_date] =>
)
[20] => Array
(
[form_field_labels_id] => 63
[form_field_labels_key] => Place_of_Purchase
[form_field_labels_name] => கொள்முதல் இடத்தில்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:13:19
[last_modified_date] =>
)
[21] => Array
(
[form_field_labels_id] => 66
[form_field_labels_key] => Country_of_Purchase
[form_field_labels_name] => கொள்முதல் நாட்டின்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:15:00
[last_modified_date] =>
)
[22] => Array
(
[form_field_labels_id] => 69
[form_field_labels_key] => Country_of_Registration
[form_field_labels_name] => பதிவு நாடு
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:18:37
[last_modified_date] =>
)
[23] => Array
(
[form_field_labels_id] => 72
[form_field_labels_key] => Product_Information
[form_field_labels_name] => தயாரிப்பு தகவல்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:22:10
[last_modified_date] =>
)
[24] => Array
(
[form_field_labels_id] => 75
[form_field_labels_key] => Model_No
[form_field_labels_name] => மாதிரி இல்லை
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:31:36
[last_modified_date] =>
)
[25] => Array
(
[form_field_labels_id] => 78
[form_field_labels_key] => Serial_No
[form_field_labels_name] => இல்லை தொடர்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:33:05
[last_modified_date] =>
)
[26] => Array
(
[form_field_labels_id] => 81
[form_field_labels_key] => Warranty_Card_No
[form_field_labels_name] => உத்தரவாதத்தை அட்டை இல்லை
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:38:00
[last_modified_date] =>
)
[27] => Array
(
[form_field_labels_id] => 84
[form_field_labels_key] => Please_fill_in_all_mandatory
[form_field_labels_name] => '*' ஆல் குறிப்பிடப்பட்டு அனைத்து கட்டாய நிரப்புக. நீங்கள் தயாராக இருக்கும் போது, வெறும் 'பதிவு' கிளிக்
[language_id] => 3
[published] => 1
[created_date] => 2013-09-13 16:41:28
[last_modified_date] =>
)
)
答案 0 :(得分:0)
聪明的代码
{* create empty arrays for array_push function *}
{assign var="keys" value=','|explode:""}
{assign var="values" value=','|explode:""}
{* cready arrays with keys and values *}
{foreach from=$given_array item=value}
{assign var=$keys value=$keys|@array_push:$value.form_field_labels_key}
{assign var=$values value=$values|@array_push:$value.form_field_labels_name}
{/foreach}
{* remove first empty elemet *}
{assign var=$keys value=$keys|@array_shift}
{assign var=$values value=$values|@array_shift}
{* combine array *}
{assign var="result" value=$keys|@array_combine:$values}
{* test output *}
{foreach from=$result item="value" key="key"}
{$key} | {$value}<br>
{/foreach}
这是我们在$ result中的内容
Personal_Information | தனிப்பட்ட தகவல்
NRIC | NRIC / இறகு / இல்லை அடையாள எண்
Title | தலைப்பு