PHP循环和数组帮助,循环结果并仅定位特定记录

时间:2015-11-12 17:14:57

标签: php arrays loops

情景:

我下载了一个Joomla扩展程序并正在编辑php文件以改变模块的布局。现在在文件的源代码中就是这段代码。

<?php if ( !empty($this->fields) ) { ?>
    foreach ($this->fields as $field)
    {
        echo RSDirectoryFilter::getInstance($field, $options)->generate();
    }
} ?>

循环显示表单字段列表并打印出字段及其选项。

我想要做的只是将一些样式应用于特定的表单字段。

该循环共打印出3个字段。我的第一步是在循环中添加一个print_r($ field),以查看$ field参数中存储的数据。每个字段都是一个数据数组

这是打印出来的

stdClass Object ( [id] => 1 [field_type_id] => 1 [name] => title [column_name] => title [form_field_name] => title [required] => 1 [published] => 1 [field_type] => title [core] => 1 [create_column] => 0 [expect_value] => 1 [properties] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( [form_caption] => Title [default_value] => [field_prepend] => [field_append] => [show_help_tip] => 1 [help_tip] => [show_help_text] => 1 [help_text_position] => block [help_text] => [readonly] => 0 [additional_html_attributes] => [id] => 1 [searchable_simple] => 1 [searchable_advanced] => textbox [searchable_advanced_caption] => Keywords [searchable_advanced_items] => [searchable_advanced_condition_type] => containing [default_validation_rule] => none [extra_accepted_chars] => [regex_syntax] => [custom_validation_rule] => [characters_limit] => 0 [validation_message] => There was an error with the title field. ) [separator] => . ) ) 
stdClass Object ( [id] => 31 [field_type_id] => 11 [name] => status [column_name] => f_31 [form_field_name] => status [required] => 1 [published] => 1 [field_type] => dropdown [core] => 0 [create_column] => 1 [expect_value] => 1 [properties] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( [id] => 31 [help_text] => [additional_html_attributes] => [credits] => 0 [default_values] => Stolen Lost Found [default_value] => [size] => 1 [multiple] => 0 [field_prepend] => [field_append] => [show_help_tip] => 1 [help_tip] => [show_help_text] => 1 [help_text_position] => block [listing_caption] => status [dependency] => 0 [items] => Stolen Lost Found [form_caption] => status [searchable_simple] => 1 [searchable_advanced] => dropdown [searchable_advanced_caption] => Listing Status [use_dependency] => 1 [use_field_items] => 1 [searchable_advanced_items] => [searchable_advanced_condition_type] => strict [validation_message] => Invalid input. ) [separator] => . ) ) 
stdClass Object ( [id] => 34 [field_type_id] => 17 [name] => date-of-incident [column_name] => f_34 [form_field_name] => date_of_incident [required] => 1 [published] => 1 [field_type] => calendar [core] => 0 [create_column] => 1 [expect_value] => 1 [properties] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( [form_caption] => Date Of Incident [listing_caption] => Date Of Incident [default_date] => [min_date] => [max_date] => [date_mask] => d F Y [time_mask] => g:i a [calendar_layout] => flat [readonly] => 0 [show_help_tip] => 0 [help_tip] => [show_help_text] => 0 [help_text_position] => block [help_text] => [additional_html_attributes] => [credits] => 0 [id] => 0 [searchable_simple] => 1 [searchable_advanced] => date_range [searchable_advanced_caption] => Date Range [searchable_advanced_items] => [searchable_advanced_condition_type] => strict [validation_message] => Invalid input. ) [separator] => . ) ) 

所以我想要定位的字段是具有[name]事件日期的字段。 (上面的第3个)

我试图修改循环所以如此定位这个特定记录

<?php if ( !empty($this->fields) ) 
{
    foreach ($this->fields as $field)
    {
        print_r($field);
        if($field[name] == "date-of-incident")
        {
            echo "Date of incident here";
        }
        else
        {
            echo "<div class='span3'>";
            echo RSDirectoryFilter::getInstance($field, $options)->generate();
            echo "</div>";
        }
    }
} ?>

但这只是崩溃了网站,但没有给出错误信息,我假设我错误地使用if($field[name] == "date-of-incident")

感谢任何帮助 感谢

1 个答案:

答案 0 :(得分:1)

请注意,print_r($field);输出表明它们是对象,即stdClass Object

因此,您使用对象表示法name而不是数组表示法来处理->属性。

<?php 
if ( !empty($this->fields) ) {
    foreach ($this->fields as $field) {
        //print_r($field);

        // here is the change
        if($field->name == "date-of-incident") {
            echo "Date of incident here";
        } else {
            echo "<div class='span3'>";
            echo RSDirectoryFilter::getInstance($field, $options)->generate();
            echo "</div>";
        }
    }
} 
?>