2016-02-02 51 views
1

這裏渴望項陣列的指數值是我的陣列的輸出找根據在笨

enter image description here

我想搜索的價值是其指數becuse每次指數在陣列 改變這裏是第二個截屏

enter image description here

我如何檢查例如我要搜索值的男性被放置在該指數 也想檢查放置值並不意味着價值存在或不存在有

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

+0

你可以按照這個來得到一個值的數組索引http://php.net/manual/en/function.key .php – Martin

+0

不清楚你在問什麼............... –

+0

添加一些例子與您的預期輸出是相當有幫助 –

回答

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); 
} 
+0

(Y)thanx對激情的幫助 –

+0

@MuhammadUsman樂於助人:) –

+1

這是很好的例子 – devpro

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] 
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"; 
} 
?>