本文整理汇总了PHP中Zend\Stdlib\ArrayUtils::inArray方法的典型用法代码示例。如果您正苦于以下问题:PHP ArrayUtils::inArray方法的具体用法?PHP ArrayUtils::inArray怎么用?PHP ArrayUtils::inArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Zend\Stdlib\ArrayUtils
的用法示例。
在下文中一共展示了ArrayUtils::inArray方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: renderOptions
/** {@inheritdoc} */
public function renderOptions(array $options, array $selectedOptions = array())
{
$escapeHtml = $this->getEscapeHtmlHelper();
$optionStrings = array();
foreach ($options as $option) {
$this->validTagAttributes = $this->validOptionAttributes;
if (\Zend\Stdlib\ArrayUtils::inArray($option, $selectedOptions)) {
$attributes = ' selected="selected"';
} else {
$attributes = '';
}
$optionStrings[] = sprintf('<option%s>%s</option>', $attributes, $escapeHtml($option));
}
return "\n" . implode("\n", $optionStrings) . "\n";
}
示例2: setOption
/**
* Option setter with validation
* If option can have the specified value then it is set, otherwise this method throws exception
*
* Tip: call it into your setter methods.
*
* @param $key
* @param $value
* @return $this
* @throws Exception\DomainException
* @throws Exception\InvalidArgumentException
*/
protected function setOption($key, $value)
{
if (!isset($this->config[$key])) {
throw new Exception\DomainException(sprintf('Option "%s" does not exist; available options are (%s)', $key, implode(', ', array_map(function ($opt) {
return '"' . $opt . '"';
}, array_keys($this->config)))));
}
if (!ArrayUtils::isList($this->config[$key], false)) {
throw new Exception\DomainException(sprintf('Option "%s" does not have a list of allowed values', $key));
}
if (!ArrayUtils::inArray($value, $this->config[$key], true)) {
throw new Exception\InvalidArgumentException(sprintf('Option "%s" can not be set to value "%s"; allowed values are (%s)', $key, $value, implode(', ', array_map(function ($val) {
return '"' . $val . '"';
}, $this->config[$key]))));
}
$this->options[$key] = $value;
return $this;
}
示例3: renderOptions
/**
* Render an array of options
*
* Individual options should be of the form:
*
* <code>
* array(
* 'value' => 'value',
* 'label' => 'label',
* 'disabled' => $booleanFlag,
* 'selected' => $booleanFlag,
* )
* </code>
*
* @param array $options
* @param array $selectedOptions Option values that should be marked as selected
* @return string
*/
public function renderOptions(array $options, array $selectedOptions = array())
{
$template = '<option %s>%s</option>';
$optionStrings = array();
$escapeHtml = $this->getEscapeHtmlHelper();
foreach ($options as $key => $optionSpec) {
$value = '';
$label = '';
$selected = false;
$disabled = false;
if (is_scalar($optionSpec)) {
$optionSpec = array('label' => $optionSpec, 'value' => $key);
}
if (isset($optionSpec['options']) && is_array($optionSpec['options'])) {
$optionStrings[] = $this->renderOptgroup($optionSpec, $selectedOptions);
continue;
}
if (isset($optionSpec['value'])) {
$value = $optionSpec['value'];
}
if (isset($optionSpec['label'])) {
$label = $optionSpec['label'];
unset($optionSpec['label']);
}
if (isset($optionSpec['selected'])) {
$selected = $optionSpec['selected'];
}
if (isset($optionSpec['disabled'])) {
$disabled = $optionSpec['disabled'];
}
if (ArrayUtils::inArray($value, $selectedOptions)) {
$selected = true;
}
if (null !== ($translator = $this->getTranslator())) {
$label = $translator->translate($label, $this->getTranslatorTextDomain());
}
$attributes = array_merge($optionSpec, compact('value', 'selected', 'disabled'));
$this->validTagAttributes = $this->validOptionAttributes;
$optionStrings[] = sprintf($template, $this->createAttributesString($attributes), $escapeHtml($label));
}
return implode("\n", $optionStrings);
}