这是我的数组的输出
我希望每次在数组中更改索引时搜索哪个值都在哪个索引上 这是第二次屏幕截图
我如何检查例如我想搜索值男性被放置在哪个索引上 并且还要检查放置的值是否存在平均值是否存在
public function ajax(){
$array = $_POST['checkbox'];
$searchTerm = "male";
if (false !== ($pos = $this->array_search2d_by_field($searchTerm, $array))) {
echo $searchTerm . " found at index " . $pos;
} else {
echo "Could not find " . $searchTerm;
}
}
function array_search2d_by_field($needle, $haystack) {
foreach ($haystack as $index => $innerArray) {
if (isset($innerArray) && $innerArray === $needle) {
return $index;
}
}
return false;
}
答案 0 :(得分:2)
您可以使用array_search功能:
示例#1 array_search()示例
<?php $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('green', $array); // $key = 2; $key = array_search('red', $array); // $key = 1; ?>
in_array功能:
示例#1 in_array()示例
<?php $os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } ?>
答案 1 :(得分:1)
试试这段代码。
这是一种基于您选中的复选框值的灵活搜索
function filterartion($array){
$array = array(
'0' => 'av',
'1' => 'nav',
'2' => 'male',
'3' => 'female',
'4' => 'A+',
'5' => 'A-',
);
// Gender
if(in_array("male", $array))
{
$gender[] = 'male';
}
if(in_array("female", $array))
{
$gender[] = 'female';
}
// Status
if(in_array("av", $array))
{
$status[] = '1';
}
if(in_array("nav", $array))
{
$status[] = '0';
}
// Group
if(in_array("A+", $array))
{
$group[] = 'A+';
}
if(in_array("A-", $array))
{
$group[] = 'A-';
}
$this->db->slect("*");
$this->db->from("members");
$this->db->where_in('gender', $gender);
$this->db->where_in('status', $status);
$this->db->where_in('blood_group', $group);
$query = $this->db->get();
$result = $query->result_array();
// return $result;
print_r($result);
}
答案 2 :(得分:0)
public function ajax(){
$array = $_POST['checkbox'];
$searchTerm = "male";
if (false !== ($pos = $this->array_search2d_by_field($searchTerm, $array))) {
echo $searchTerm . " found at index " . $pos;
} else {
echo "Could not find " . $searchTerm;
}
}
function array_search2d_by_field($needle, $haystack) {
foreach ($haystack as $index => $innerArray) {
if (isset($innerArray) && $innerArray === $needle) {
return $index;
}
}
return false;
}
[![enter image description here][1]][1]