我有一个关联数组,它分配给 smarty 模板。在那里,我不明白如何访问一个特定的密钥。我对此进行了研究,但无法获得具体的解决方案。场景如下:
我的 php 数组命名$all_class_subjects
如下:
Array
(
[0] => Array
(
[class_id] => 1
[class_name] => I
[I] => Array
(
[cs_map_id] =>
[cs_class_id] =>
[cs_subject_id] =>
[subjects] =>
)
)
[1] => Array
(
[class_id] => 2
[class_name] => II
[II] => Array
(
[cs_map_id] =>
[cs_class_id] =>
[cs_subject_id] =>
[subjects] =>
)
)
[2] => Array
(
[class_id] => 3
[class_name] => III
[III] => Array
(
[cs_map_id] =>
[cs_class_id] =>
[cs_subject_id] =>
[subjects] =>
)
)
[3] => Array
(
[class_id] => 4
[class_name] => IV
[IV] => Array
(
[cs_map_id] =>
[cs_class_id] =>
[cs_subject_id] =>
[subjects] =>
)
)
[4] => Array
(
[class_id] => 5
[class_name] => V
[V] => Array
(
[cs_map_id] => 1
[cs_class_id] => 5
[cs_subject_id] => 3
[subjects] => Array
(
[subject_name] => Mathematics
)
)
)
[5] => Array
(
[class_id] => 6
[class_name] => VI
[VI] => Array
(
[cs_map_id] => 2
[cs_class_id] => 6
[cs_subject_id] => 4
[subjects] => Array
(
[subject_name] => Biology
)
)
)
)
现在我想在 smarty 模板中打印这个数组的值。代码片段如下 它可能有一些 HTML 错误:
{if $all_class_subjects}
{foreach from=$all_class_subjects item=class_subject_data}
<tr>
<td width="20%">{$class_subject_data.class_name|capitalize}</td>
<td width="40%">
{foreach from=class_subject_data item=subject}
{if $subject.cls_sub_subject_name}
{$class_subject_data.cs_subject_id}
我的问题是我想检查数组是否
[I] => Array
(
[cs_map_id] =>
[cs_class_id] =>
[cs_subject_id] =>
[subjects] =>
)
是否为空。您可以看到除了最后两个元素之外,所有数组元素的这些字段都为空。所以简而言之,我的问题是如何访问包含上述字段的内部数组作为数组的索引,即键(I、II、III、IV 等)正在改变?返回数组的 PHP 函数如下:
function GetAllClassSubjects( $cur_page, $sort_col_id, $desc_or_asc, $request) {
global $grid;
$grid->mSqlArr['fields'] = " classes.class_id, classes.class_name" ;
$grid->mSqlArr['tables'] = TBL_CLASSES." AS classes ";
$grid->mSqlArr['orderby'] = "classes.class_order";
$grid->mSqlArr['where'] = "";
$grid->mCurPage = $cur_page;
$grid->mRecPerPage = REC_PER_PAGE; //default is 20
$grid->mSortColumns->mColsArr = array('class_id');
$grid->mSortColumns->mDefaultOrderStr = "";
$grid->mSortColumns->mCurColNo = $sort_col_id;
$grid->mSortColumns->mCurSortOrder = "DESC";
$class_data = $grid->GetData();
//print_p($class_data);
$grid->mSqlArr['fields'] = " class_subject_mapping.cs_map_id, class_subject_mapping.cs_class_id, class_subject_mapping.cs_subject_id" ;
$grid->mSqlArr['tables'] = TBL_CLASS_SUBJECT_MAPPING." AS class_subject_mapping ";
$grid->mSqlArr['orderby'] = "";
$grid->mSqlArr['where'] = "";
$grid->mCurPage = $cur_page;
$grid->mRecPerPage = REC_PER_PAGE; //default is 20
$grid->mSortColumns->mColsArr = array('cs_map_id');
$grid->mSortColumns->mDefaultOrderStr = "";
$grid->mSortColumns->mCurColNo = $sort_col_id;
$grid->mSortColumns->mCurSortOrder = "DESC";
$mapping_data = $grid->GetData();
//print_p($mapping_data);
foreach($class_data as $class)
{
$flag = 0;
$class_id = $class['class_id'];
$class_name = $class['class_name'];
foreach($mapping_data as $class_subject)
{
$cs_map_id = $class_subject['cs_map_id'];
$cs_class_id = $class_subject['cs_class_id'];
$cs_subject_id = $class_subject['cs_subject_id'];
if($class_id == $cs_class_id)
{
$flag = 1;
$class_data[$class_id-1][$class_name]['cs_map_id'] = $cs_map_id;
$class_data[$class_id-1][$class_name]['cs_class_id'] = $cs_class_id;
$class_data[$class_id-1][$class_name]['cs_subject_id'] = $cs_subject_id;
$sql = " SELECT subject_name";
$sql .= " FROM ".TBL_SUBJECTS." AS subjects";
$sql .= " WHERE subjects.subject_id = ".$cs_subject_id;
$sql .= " order by subject_order";
$this->mDb->Query( $sql);
$class_subjects_data = $this->mDb->FetchArray();
//print_p($class_subjects_data);
foreach($class_subjects_data as $sub)
{
$class_data[$class_id-1][$class_name]['subjects'] = $sub;
}
}
}
if($flag == 0)
{
$class_data[$class_id-1][$class_name]['cs_map_id'] = "";
$class_data[$class_id-1][$class_name]['cs_class_id'] = "";
$class_data[$class_id-1][$class_name]['cs_subject_id'] = "";
$class_data[$class_id-1][$class_name]['subjects'] = "";
}
}
print_p($class_data);
//ksort($cls_sub_data[$cs_map_id]);
//print_p($cls_sub_data);
list( $sort_url_param, $sort_col_array, $pagination_array ) =
$grid->GetUrlParam( PAGE_OFFSET, PHP_SELF."?op=".$query_string."&", "col", 'order', 'page' );
return array($class_data, $sort_url_param, $sort_col_array, $pagination_array );
}
请帮我解决这个问题。提前致谢。