【发布时间】:2023-03-26 00:25:02
【问题描述】:
以下是我在 PHP 中的数组标题 $test_data:
Array
(
[1] => Array
(
[test_pack_id] => 84880f321fc957f9b28b702d5a43f57b
[test_pack_name] => CPT General Economics
[test_pack_desc] => This Package contains <b>16 chapterwise testlets</b> of 15 minutes each, <b>8 chapterwise tests </b>of 1 hour each and <b>3 full syllabus tests</b> of 1 hour covering syllabus of CPT General Economics.<br>
<b>Total Tests in this Package : 27</b>
[test_pack_type_id] => 7
[test_pack_image] =>
[test_pack_validity_year] => 0
[test_pack_validity_month] => 6
[test_pack_validity_days] => 0
[test_pack_plan] => paid
[test_pack_price] => 359.00
[test_pack_no_tests] => 0
[test_pack_publish] => yes
[test_pack_code] =>
[test_pack_sold] => 0
[test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
[test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
[test_pack_created_date] => 1338298119
[test_pack_updated_date] => 1343564534
[test_pack_purchase_date] => 0
[test_details] => Array
(
[0] => Array
(
[test_id] => 580
[test_name] => CPT Testlet : Demand & Supply 1
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[1] => Array
(
[test_id] => 581
[test_name] => CPT Test : Micro Economics
[test_max_score] => 50.000
[test_no_questions] => 50
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[2] => Array
(
[test_id] => 583
[test_name] => CPT Testlet : Micro Economics 1
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[3] => Array
(
[test_id] => 582
[test_name] => CPT Testlet : Micro Economics 2
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] => 0.00
[avg_score] => 0
[lower_score] => 0.00
)
[4] => Array
(
[test_id] => 579
[test_name] => CPT Testlet : Demand & Supply 2
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[5] => Array
(
[test_id] => 578
[test_name] => CPT Test : Demand & Supply
[test_max_score] => 50.000
[test_no_questions] => 50
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] => 0.00
[avg_score] => 0
[lower_score] => 0.00
)
[6] => Array
(
[test_id] => 577
[test_name] => CPT Testlet : Production & Cost 1
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[7] => Array
(
[test_id] => 576
[test_name] => CPT Testlet : Production & Cost 2
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] => 0.00
[avg_score] => 0
[lower_score] => 0.00
)
[8] => Array
(
[test_id] => 575
[test_name] => CPT Test : Production & Cost
[test_max_score] => 50.000
[test_no_questions] => 50
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] => 0.00
[avg_score] => 0
[lower_score] => 0.00
)
[9] => Array
(
[test_id] => 574
[test_name] => CPT Testlet : Price Determination 1
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] => 0.00
[avg_score] => 0
[lower_score] => 0.00
)
[10] => Array
(
[test_id] => 573
[test_name] => CPT Testlet : Price Determination 2
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[11] => Array
(
[test_id] => 572
[test_name] => CPT Test : Price Determination
[test_max_score] => 50.000
[test_no_questions] => 50
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[12] => Array
(
[test_id] => 571
[test_name] => CPT Testlet : Indian Economy - A Profile 1
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[13] => Array
(
[test_id] => 570
[test_name] => CPT Testlet : Indian Economy - A Profile 2
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[14] => Array
(
[test_id] => 569
[test_name] => CPT Test : Indian Economy - A Profile
[test_max_score] => 50.000
[test_no_questions] => 50
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] =>
[avg_score] => 0
[lower_score] =>
)
[15] => Array
(
[test_id] => 568
[test_name] => CPT Testlet : Aspects of Indian Economy 1
[test_max_score] => 40.000
[test_no_questions] => 10
[test_attempt] => 0
[test_not_attempt] => 0
[top_score] => 0.00
[avg_score] => 0
[lower_score] => 0.00
)
)
)
)
Now by using following statement I'm assigning this array to smarty template:
$smarty->assign('test_data', $test_data);
现在我想在我的 smarty 模板中打印所有测试的值,即我必须为数组 $test_data['test_details'] 应用循环。我应用了它,但它在 smarty 中无法正常工作。给出一些模糊的结果。我不明白我在哪里犯了错误。你能在这方面帮助我吗?我的代码如下:
<select name="test_id" id="test_id">
<option value="all">All</option>
{foreach from=$test_data item=test key=key}
{foreach from=$test.test_details item=test_detail key=key}
<option value="{$test_detail.test_id}" {if $test_id==$test_detail.test_id} selected="selected"{/if}>{$test_detail.test_name}</option>
{/foreach}
{/foreach}
</select>
【问题讨论】:
-
尝试命名你的循环,
{foreach name = "outer_loop" [...]}和{foreach name = "inner_loop" [...]}看看是否有帮助 -
结果模糊是什么意思?另外,您使用的是哪个版本的 smarty——版本 3 或 2?
标签: php arrays foreach smarty associative-array