我试图将一些复选框值提取到数组中。我的问题是数组($ is_types)。我期待$ is_types是一个数组,比如数组(一,二,三):
<?php
if (!is_array($types)) {
$types = array();
}
$filtered_array = array_filter($types);
$is_types = in_array($type, $filtered_array);
$output = _get_array(array($is_types), $bla, $bla2);
?>
更新 我需要重新说明我想要实现的目标。 我有一些带有选项的复选框:一,二,三等。只有当我检查它们时,才应存储选项。使用$ is_types检查存储的内容就足够了,返回bool。 然后我需要根据这些选中的复选框进行其他查询来聚合内容。那就是如果我有一个数组(一,二,三),基于过滤的复选框,然后运行查询
UPDATE2:
function _get_array($type, $view_mode, $limit = NULL) {
$node = menu_get_object();
$build = array();
$query = new EntityFieldQuery();
$created = isset($node) ? $node->created : 'now';
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', $type, is_array($type) ? 'IN' : '=')
->propertyCondition('status', 1)
->propertyOrderBy('created', 'DESC')
->propertyCondition('created', $created, '<');
if ($limit != NULL) {
$query->range(0, $limit);
}
$result = $query->execute();
if (!empty($result['node'])) {
$nodes = entity_load('node', array_keys($result['node']));
$build[] = node_view_multiple($nodes, $view_mode);
}
return !empty($build) ? $build : array();
}
复选框只是带有一些值的复选框。 所以查询_get_array(array($ is_types),$ bla,$ bla2);应加载按这些复选框值过滤的所有节点。
任何暗示都会非常感激。感谢
UPDATE3: 谢谢大家。答案一直都在那里。我需要的只是:$ filtered_array
答案 0 :(得分:1)
<input type="checkbox" name="test" value="one"/>
<input type="checkbox" name="test" value="two"/>
<input type="checkbox" name="test" value="three"/>
在服务器上,您需要一个包含一个,两个或三个的数组,具体取决于选择的数组。
基本上,您必须在'test'中添加方括号,以告诉PHP将test
视为数组,如下所示:
<input type="checkbox" name="test[]" value="one"/>
<input type="checkbox" name="test[]" value="two"/>
<input type="checkbox" name="test[]" value="three"/>
在服务器上,您只需按以下方式访问:
print_r( $_REQUEST['test'] );
请注意,如果未选择任何选项,则不会获得阵列。相反,请执行以下操作:
$options = isset( $_REQUEST['test'] ) ? $_REQUEST['test'] : array();
要确保所有变量合法,请执行以下操作:
$legit = array('one','two','three');
foreach($options as $n=>$option)
if(!in_array( $option, $legit ))
unset( $options[ $n ] );