在Array PHP中搜索

时间:2014-05-30 05:13:22

标签: php arrays

我想在以下数组中创建搜索功能:

array(      
    array("key" => "net_sale",         "value" => "NET SALE CRTN"),
    array("key" => "productive_calls", "value" => "Productive Calls"),
    array("key" => "drop_size_value",  "value" => "DROP SIZE VALUE"),
    array("key" => "sku",              "value" => "SKU PER BILL"),
    array("key" => "net_amount",       "value" => "Net Amount"),
    array("key" => "drop_size_crtn",   "value" => "DROP SIZE CRTN"),
    array("key" => "productive_pops",  "value" => "PRODUCTIVE POPS"),
    array("key" => "scheduled_pops",   "value" => "SCHEDULED POPS") 
);

此函数将返回搜索到的任何键的位置。 (例如,对于搜索“net_amount”,它将返回4)

8 个答案:

答案 0 :(得分:2)

function search($array,$key_value) {
   foreach($array AS $key => $value) {
     if($value['key'] == $key_value) return $key;
   }
}
echo search($arr,'net_amount'); //$arr contains the array you gave as example

答案 1 :(得分:0)

创建一个数组,其中键实际上是一个键,值是值。

$map = array();
for ($i = 0; $i < count($values); $i++) {
    $value = $values[$i];
    $map[$value['key']] = $i;
}

$net_amount_pos = $map['net_amount'];

答案 2 :(得分:0)

来自php manual

中的评论
<?php
function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}
?>

但是在您的情况下,有一个更简单的解决方案。而不是使用“密钥”和“价值”,这是一种浪费,格式化您的数组:

array('net_sale' =>'NET SALE CRTN','productive_calls'=>'Productive Calls') //etc.

然后你可以直接拿出钥匙而不需要搜索它......

答案 3 :(得分:0)

我同意对这个问题的评论,我不相信你当前存储数据的方式是最好的。尽管如此,只需遍历每个数组并检查是否包含您要搜索的字符串

function searchArrays($search){
    $return = array();
    $data = array(      
        array("key" => "net_sale",         "value" => "NET SALE CRTN"),
        array("key" => "productive_calls", "value" => "Productive Calls"),
        array("key" => "drop_size_value",  "value" => "DROP SIZE VALUE"),
        array("key" => "sku",              "value" => "SKU PER BILL"),
        array("key" => "net_amount",       "value" => "Net Amount"),
        array("key" => "drop_size_crtn",   "value" => "DROP SIZE CRTN"),
        array("key" => "productive_pops",  "value" => "PRODUCTIVE POPS"),
        array("key" => "scheduled_pops",   "value" => "SCHEDULED POPS") 
    );

    foreach($data as $k => $v){
        if(stristr($v['key'], $search) || stristr($v['value'], $search)){
            $return[] = $k;
        }
    }

    return $return;
};
print_r(searchArrays('net'));

答案 4 :(得分:0)

这是以下代码:

$test  = array(      
    array("key" => "net_sale",         "value" => "NET SALE CRTN"),
    array("key" => "productive_calls", "value" => "Productive Calls"),
    array("key" => "drop_size_value",  "value" => "DROP SIZE VALUE"),
    array("key" => "sku",              "value" => "SKU PER BILL"),
    array("key" => "net_amount",       "value" => "Net Amount"),
    array("key" => "drop_size_crtn",   "value" => "DROP SIZE CRTN"),
    array("key" => "productive_pops",  "value" => "PRODUCTIVE POPS"),
    array("key" => "scheduled_pops",   "value" => "SCHEDULED POPS") 
);
$index = keyval($test,"net_amount");

echo $index;
function keyval($test,$val)
{
$i = null;
    foreach($test as $index=>$t)
    {
        //echo $t['key'];
        if($t['key']==$val){ $i = $index;}
    }
    return $i;
}

答案 5 :(得分:0)

试试这个 -

定义功能

function checkPosition($needle, $arrData)
{
  $i=0;
   foreach($arrData as $data){

      if(in_array($needle,$data))
        { return $i; }

      $i++;
   }


}

调用函数 -

echo checkPosition('net_amount',$test);
Output : 4

答案 6 :(得分:0)

这是另一种方式:

$array = array(      
    array("key" => "net_sale",         "value" => "NET SALE CRTN"),
    array("key" => "productive_calls", "value" => "Productive Calls"),
    array("key" => "drop_size_value",  "value" => "DROP SIZE VALUE"),
    array("key" => "sku",              "value" => "SKU PER BILL"),
    array("key" => "net_amount",       "value" => "Net Amount"),
    array("key" => "drop_size_crtn",   "value" => "DROP SIZE CRTN"),
    array("key" => "productive_pops",  "value" => "PRODUCTIVE POPS"),
    array("key" => "scheduled_pops",   "value" => "SCHEDULED POPS") 
);

function search($term, $array) {
    for ($i=0; $i<count($array); $i++) {
        if ($array[$i]['key'] == $term) return $i;
    }
}

echo search("net_amount", $array);

here is a demo

答案 7 :(得分:0)

$data=array(
    array("key" => "net_sale",         "value" => "NET SALE CRTN"),
    array("key" => "productive_calls", "value" => "Productive Calls"),
    array("key" => "drop_size_value",  "value" => "DROP SIZE VALUE"),
    array("key" => "sku",              "value" => "SKU PER BILL"),
    array("key" => "net_amount",       "value" => "Net Amount"),
    array("key" => "drop_size_crtn",   "value" => "DROP SIZE CRTN"),
    array("key" => "productive_pops",  "value" => "PRODUCTIVE POPS"),
    array("key" => "scheduled_pops",   "value" => "SCHEDULED POPS")
);

$search_key="sku";


for($count=0; $count<=count($data); ++$count)
{

    if(strcmp($data[$count]["key"], $search_key)==0)
    {
        echo $data[$count]["value"];
    }

}