0

I have following array :

    [2] => Array
        (
            [main_type] => amount
            [main_value] => amount
        )

    [3] => Array
        (
            [main_type] => amount
            [main_value] => code
        )

    [4] => Array
        (
            [main_type] => hello
            [main_value] => amount
        )

Now I want to find that if main_type = hello and main_value = amount is exists or not in entire array. How can i find through entire array.

I have tried using array_search but with that i am able to search in one column but not for more than 1.

3
  • You can do it by Foreach: $found_in_array = false; foreach($arr as $dt){ if($dt["main_type"] == "sendfee" && $dt["main_value"] == "amount"){ $found_in_array = true; break; } } if($found_in_array == true){ echo "Found"; } Commented Dec 21, 2021 at 5:45
  • I do not want to use foreach. Commented Dec 21, 2021 at 5:46
  • 1
    Check this: stackoverflow.com/questions/32333436/… Commented Dec 21, 2021 at 5:48

2 Answers 2

0

You can use array_filter for checking:

See Example Code:

$var1 = 'hello';
$var2 = 'amount';                                                           ▼
$filtered_array = array_filter($your_array, function($val) use($var1, $var2){
  return ($val['main_type']==$var1 and $val['main_value']==$var2);
});
Sign up to request clarification or add additional context in comments.

Comments

0

Please use PHP's array_filter function with callback. Here's array_filter

$var1 = 'hello';
$var2 = 'amount';
$result = array_filter($arr, function ($var) use ($var1, $var2) {
    return ($var['main_type'] == $var1 && $var['main_value'] == $var2);
});
//print_r($result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.