gpt4 book ai didi

php - 从查询中分解数组

转载 作者:行者123 更新时间:2023-11-29 09:00:22 25 4
gpt4 key购买 nike

我无法让数组像这样显示。

$womensLevels = array('WEXHIB' => 'Exhib Camp/Test', 'WLEVEL01' => 'Level 1', 'WLEVEL02' => 'Level 2', 'WLEVEL03' => 'Level 3', 'WLEVEL04' => 'Level 4', 'WLEVEL05' => 'Level 5', 'WLEVEL06' => 'Level 6', 'WLEVEL07' => 'Level 7', 'WLEVEL08' => 'Level 8', 'WLEVEL09' => 'Level 9', 'WLEVEL10' => 'Level 10', 'WOPEN' => 'Open', 'WPREPOPT' => 'Prep OPT', 'WTOPS' => 'TOPS');

当我执行查询并将其放入我的 View (html)中时,结果就像这样。我只希望代码键和描述键与这些值一起显示。

[0]=>
array(2) {
["Code"]=>
string(4) "IW01"
["Description"]=>
string(13) "Intro Level 1"
}
[1]=>
array(2) {
["Code"]=>
string(4) "IW02"
["Description"]=>
string(13) "Intro Level 2"
}
[2]=>
array(2) {
["Code"]=>
string(4) "IW03"
["Description"]=>
string(13) "Intro Level 3"
}

这是我的疑问。

这就是数组的来源。

$womensLevels = sprintf("SELECT `Code`,`Description` FROM `mb_ro_type_codes` where `Program` = '%s'", $sanction->Program);

$smarty->assign('womensLevels', $db->query($womensLevels));

这是我从 Controller 将变量放入的 html。

{html_checkboxes name='code' options=$womensLevels selected=$checked}

我想我想问的是如何分解我的数组?

最佳答案

您必须循环数据库查询结果并以您想要的格式构建另一个数组,而不是直接将查询结果传递给模板。像这样的事情:

$results = $db->query($womensLevels);
$formattedResults = array();
foreach($results as $result) {
formattedResults[$result['Code']] = $result['Description'];
}
$smarty->assign('womensLevels', $formattedResults);

关于php - 从查询中分解数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8810217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com