Shehzad Bilal
Shehzad Bilal

Reputation: 144

Search a 2d array for a qualifying column value and return its first level key

I want to make a search function in the following 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") 
);

This function will return the position for any key searched. (for example for search "net_amount" it will return 4)

Upvotes: 1

Views: 147

Answers (8)

WizKid
WizKid

Reputation: 4908

Create an array where the key is actually a key and the value is the value.

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

$net_amount_pos = $map['net_amount'];

Upvotes: 0

DasturchiUZ
DasturchiUZ

Reputation: 69

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

}

Upvotes: 0

benomatis
benomatis

Reputation: 5643

Here is another way:

$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);

And here is a demo

Upvotes: 0

Jitendra Yadav
Jitendra Yadav

Reputation: 896

try this-

Define a Function

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

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

      $i++;
   }


}

Calling a Function-

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

Upvotes: 0

Manwal
Manwal

Reputation: 23836

this is following code:

$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;
}

Upvotes: 0

Adam Merrifield
Adam Merrifield

Reputation: 10407

I agree with the comments on the question, I don't believe the way you're currently storing the data is the best possible. Nonetheless just loop through each array and check if either contains the string you're searching for

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'));

Upvotes: 0

Tanatos
Tanatos

Reputation: 1917

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

Upvotes: 2

serakfalcon
serakfalcon

Reputation: 3531

From the comments in the 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;
}
?>

However in your case there is a simpler solution. Instead of using "key" and "value" which is kind of a waste, format your array like:

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

then you can pull out the key directly instead of needing to search for it...

Upvotes: 0

Related Questions