0

Is it possible to search a string in array with keys or values

Like string apple search in -

array(
  "a"=>"banana",
  "apple"=>"fruit",
  'b'=>"apple"
)

when apple is matched in array keys or values then return true otherwise false

  • Try searching your question before asking it on here https://www.w3schools.com/Php/func_array_search.asp – Tom Aug 30 '17 at 11:15
  • It is possible in PHP to find a value or a key in an array. Take a look at the big list of PHP [array functions](http://php.net/manual/en/ref.array.php). Focus on [`in_array()`](http://php.net/manual/en/function.in-array.php), [`isset()`](http://php.net/manual/en/function.isset.php), [`array_key_exists()`](http://php.net/manual/en/function.array-key-exists.php) and [`array_keys()`](http://php.net/manual/en/function.array-keys.php). – axiac Aug 30 '17 at 11:17
  • 1
    The duplicate this thread is closed for is incorrect. The linked thread is about Ruby, question is in php. – Andreas Aug 30 '17 at 12:11
  • 1
    @Andreas fixed that. – axiac Aug 30 '17 at 12:39

5 Answers5

3

when "apple" is matched in above array in keys or values then return true otherwise false

Short "one-liner":

$has_apple = key_exists('apple', $arr) || in_array('apple', $arr);
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1
<?php

$data = array("a"=>"banana","apple"=>"fruit",'b'=>"apple");

function key_or_value_exists($needle, $haystack) {
    return array_key_exists($needle, $haystack) || in_array($needle, $haystack);
}

var_dump(key_or_value_exists('apple', $data));
var_dump(key_or_value_exists('banana', $data));
var_dump(key_or_value_exists('papaya', $data));

Output:

boolean true
boolean true
boolean false
Progrock
  • 7,373
  • 1
  • 19
  • 25
0

You can use array_search() function to do -

Example : Search by Value {Index Array}

$array = array(
   'blue',
   'red',
   'green',
   'red'
);
$key   = array_search('green', $array); #$key = 2;
$key   = array_search('red', $array);   #$key = 1;

Example : Search by Value {Associative Array}

$array = array(
   'A'=>'blue',
   'B'=>'red',
   'C'=>'green',
   'D'=>'red'
);
$key   = array_search('green', $array); #$key = 'C';
$key   = array_search('red', $array);   #$key = 'D';
$key   = array_search('NO', $array);    #$key = false;
Sumon Sarker
  • 2,707
  • 1
  • 23
  • 36
0

Isset() can do it.

$arr =array("a" => "banana","apple" => "fruit",'b' => "apple");

If(isset($arr["apple"]) || array_search("apple", $arr)){
     Echo "true";
}Else{
     Echo "false";
}

https://3v4l.org/65jSS

Edit noticed you needed in value too.
For that you can use array_search(needle, haystack)

If it is the bool value you are looking for you can also do:

$bool = (bool)isset($arr["apple"]) || array_search("apple", $arr);
Andreas
  • 23,610
  • 6
  • 30
  • 62
0

Finding if a string is a key or a value in an array is as easy as calling array_key_exists() and in_array(). If one of them returns true then the string is used inside the array:

function findInArray(array $array, $item)
{
    return array_key_exists($item, $array) || in_array($item, $array);
}

// Test
$input = array(
    'a'     => 'banana',
    'apple' => 'fruit',
    'b'     => 'apple',
    'pear'  => 'fruit',
);

// 'banana' is a value in the array
printf("banana: %s\n", findInArray($input, 'banana') ? 'Yes' : 'No');
// 'apple' is both a key and a value in the array
printf("apple:  %s\n", findInArray($input, 'apple') ? 'Yes' : 'No');
// 'pear' is a key in the array
printf("pear:   %s\n", findInArray($input, 'pear') ? 'Yes' : 'No');
// 'mango' is not present in the array, either as key or value
printf("mango:  %s\n", findInArray($input, 'mango') ? 'Yes' : 'No');

The output (as expected) is:

banana: Yes
apple:  Yes
pear:   Yes
mango:  No
axiac
  • 68,258
  • 9
  • 99
  • 134