如何通过键访问关联数组中的值
How to access value by key in associative array
我制作了一个php模板。我在index.php(主页)的代码中使用模板的方式是这样的:
<?php
require 'scripts/back_end/views/country_select.php';
require 'scripts/back_end/views/view.php';
echo View::render('select_template.php');
?>
现在这会导致此错误:
[17-Feb-2016 05:19:34 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 3
[17-Feb-2016 05:19:34 Europe/Berlin] PHP Fatal error: Cannot declare class CountrySelect, because the name is already in use in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/country_select.php on line 3
我认为这是由 require
-ing country_select.php 在 index.php 和 select_template.php 中引起的。我认为注释掉 index.php 中的那个是解决这个问题的方法。这是注释掉顶部 require
时得到的输出 html
(请参阅问题底部以获得所需的 html
输出)
<select data-bind="options: 'options',
optionsText: 'optionsText',
optionsValue: 'optionsValue',
value: value,
optionsCaption: 'caption'"><option value="">caption</option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option>
</select>
注释掉顶部时出现问题require
:
我正在尝试访问此 class:
中选项数组的值
<?php
class CountrySelect {
static $template = 'select_template.php';
public static function display() {
if ( class_exists( 'View' ) ) {
// Get the full path to the template file.
$templatePath = dirname( __FILE__ ) . static::$template;
$viewData = array(
"options" => '_countries',
"optionsText" => 'name',
"optionsValue" => 'geonameId',
"value" => 'selectedCountry',
"caption" => 'Country'
);
// Return the rendered HTML
return View::render( $templatePath, $viewData );
}
else {
return "You are trying to render a template, but we can't find the View Class";
}
}
}
?>
我在 PHP 控制台中收到这些错误。
[17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable:
options in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 3 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of
undefined constant options - assumed 'options' in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 11 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined
variable: options in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 11 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of
undefined constant optionsText - assumed 'optionsText' in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 12 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined
variable: options in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 12 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of
undefined constant optionsValue - assumed 'optionsValue' in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 13 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined
variable: options in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 13 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of
undefined constant value - assumed 'value' in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 14 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined
variable: options in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 14 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of
undefined constant caption - assumed 'caption' in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 15 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined
variable: options in
/Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php
on line 15
访问数组的模板:
<?php
print_r($options);
include 'country_select.php';
?>
<div class="form-group col-sm-6">
<div class="select">
<span class="arr"></span>
<select data-bind="options: '<? echo $options.options ?>',
optionsText: '<? echo $options.optionsText ?>',
optionsValue: '<? echo $options.optionsValue ?>',
value: <? echo $options.value ?>,
optionsCaption: '<? echo $options.caption ?>'">
</select>
</div>
</div>
按键访问关联数组值的正确方法是什么?
这是具有渲染功能的 view.php 文件:
<?php
/** View.php **/
class View {
/**
* -------------------------------------
* Render a Template.
* -------------------------------------
*
* @param $filePath - include path to the template.
* @param null $viewData - any data to be used within the template.
* @return string -
*
*/
public static function render( $filePath, $viewData = null ) {
// Was any data sent through?
( $viewData ) ? extract( $viewData ) : null;
print_r($viewData);
ob_start();
include ( $filePath );
$template = ob_get_contents();
ob_end_clean();
return $template;
}
}
?>
我正在使用 this tutorial
我希望我的文字 html 模板是这样的
<div class="form-group col-sm-6">
<div class="select">
<span class="arr"></span>
<select data-bind="options: _regions,
optionsText: 'name',
optionsValue: 'geonameId',
value: selectedCountry,
optionsCaption: 'Country'">
</select>
</div>
</div>
编辑:问题已经过大量修改,因此从头开始完整回答。
- Index.php
您CountrySelect
class已经在使用select_template.php
所以你在 index.php 中的代码应该是
<?php
require 'scripts/back_end/views/country_select.php';
require 'scripts/back_end/views/view.php';
echo CountrySelect::display();
?>
- 您的模板:
你的 CountrySelect
class 已经包含了你的模板,你也将你的 $viewData 作为
$viewData = array(
"options" => '_countries',
"optionsText" => 'name',
"optionsValue" => 'geonameId',
"value" => 'selectedCountry',
"caption" => 'Country'
);
使用 extract()
导出到 View::render() 范围
像这样
( $viewData ) ? extract( $viewData ) : null;
这会将 $viewData 的每个键创建为一个变量名,所以现在一旦提取完成,您将有 5 个变量
$options, $optionsText, $optionsValue, $value, $caption
所以你的最终模板应该像
<div class="form-group col-sm-6">
<div class="select">
<span class="arr"></span>
<select data-bind="options: '<?php echo $options ?>',
optionsText: '<?php echo $optionsText ?>',
optionsValue: '<?php echo $optionsValue ?>',
value: <?php echo $value ?>,
optionsCaption: '<?php echo $caption ?>'">
</select>
</div>
</div>
我制作了一个php模板。我在index.php(主页)的代码中使用模板的方式是这样的:
<?php
require 'scripts/back_end/views/country_select.php';
require 'scripts/back_end/views/view.php';
echo View::render('select_template.php');
?>
现在这会导致此错误:
[17-Feb-2016 05:19:34 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 3
[17-Feb-2016 05:19:34 Europe/Berlin] PHP Fatal error: Cannot declare class CountrySelect, because the name is already in use in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/country_select.php on line 3
我认为这是由 require
-ing country_select.php 在 index.php 和 select_template.php 中引起的。我认为注释掉 index.php 中的那个是解决这个问题的方法。这是注释掉顶部 require
时得到的输出 html
(请参阅问题底部以获得所需的 html
输出)
<select data-bind="options: 'options',
optionsText: 'optionsText',
optionsValue: 'optionsValue',
value: value,
optionsCaption: 'caption'"><option value="">caption</option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option><option value=""></option>
</select>
注释掉顶部时出现问题require
:
我正在尝试访问此 class:
中选项数组的值 <?php
class CountrySelect {
static $template = 'select_template.php';
public static function display() {
if ( class_exists( 'View' ) ) {
// Get the full path to the template file.
$templatePath = dirname( __FILE__ ) . static::$template;
$viewData = array(
"options" => '_countries',
"optionsText" => 'name',
"optionsValue" => 'geonameId',
"value" => 'selectedCountry',
"caption" => 'Country'
);
// Return the rendered HTML
return View::render( $templatePath, $viewData );
}
else {
return "You are trying to render a template, but we can't find the View Class";
}
}
}
?>
我在 PHP 控制台中收到这些错误。
[17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 3 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant options - assumed 'options' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 11 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 11 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant optionsText - assumed 'optionsText' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 12 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 12 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant optionsValue - assumed 'optionsValue' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 13 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 13 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant value - assumed 'value' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 14 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 14 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Use of undefined constant caption - assumed 'caption' in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 15 [17-Feb-2016 05:15:48 Europe/Berlin] PHP Notice: Undefined variable: options in /Applications/MAMP/htdocs/its_vegan/scripts/back_end/views/select_template.php on line 15
访问数组的模板:
<?php
print_r($options);
include 'country_select.php';
?>
<div class="form-group col-sm-6">
<div class="select">
<span class="arr"></span>
<select data-bind="options: '<? echo $options.options ?>',
optionsText: '<? echo $options.optionsText ?>',
optionsValue: '<? echo $options.optionsValue ?>',
value: <? echo $options.value ?>,
optionsCaption: '<? echo $options.caption ?>'">
</select>
</div>
</div>
按键访问关联数组值的正确方法是什么?
这是具有渲染功能的 view.php 文件:
<?php
/** View.php **/
class View {
/**
* -------------------------------------
* Render a Template.
* -------------------------------------
*
* @param $filePath - include path to the template.
* @param null $viewData - any data to be used within the template.
* @return string -
*
*/
public static function render( $filePath, $viewData = null ) {
// Was any data sent through?
( $viewData ) ? extract( $viewData ) : null;
print_r($viewData);
ob_start();
include ( $filePath );
$template = ob_get_contents();
ob_end_clean();
return $template;
}
}
?>
我正在使用 this tutorial
我希望我的文字 html 模板是这样的
<div class="form-group col-sm-6">
<div class="select">
<span class="arr"></span>
<select data-bind="options: _regions,
optionsText: 'name',
optionsValue: 'geonameId',
value: selectedCountry,
optionsCaption: 'Country'">
</select>
</div>
</div>
编辑:问题已经过大量修改,因此从头开始完整回答。
- Index.php
您CountrySelect
class已经在使用select_template.php
所以你在 index.php 中的代码应该是
<?php
require 'scripts/back_end/views/country_select.php';
require 'scripts/back_end/views/view.php';
echo CountrySelect::display();
?>
- 您的模板:
你的 CountrySelect
class 已经包含了你的模板,你也将你的 $viewData 作为
$viewData = array(
"options" => '_countries',
"optionsText" => 'name',
"optionsValue" => 'geonameId',
"value" => 'selectedCountry',
"caption" => 'Country'
);
使用 extract()
导出到 View::render() 范围
像这样
( $viewData ) ? extract( $viewData ) : null;
这会将 $viewData 的每个键创建为一个变量名,所以现在一旦提取完成,您将有 5 个变量
$options, $optionsText, $optionsValue, $value, $caption
所以你的最终模板应该像
<div class="form-group col-sm-6">
<div class="select">
<span class="arr"></span>
<select data-bind="options: '<?php echo $options ?>',
optionsText: '<?php echo $optionsText ?>',
optionsValue: '<?php echo $optionsValue ?>',
value: <?php echo $value ?>,
optionsCaption: '<?php echo $caption ?>'">
</select>
</div>
</div>