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

4
  • Try searching your question before asking it on here w3schools.com/Php/func_array_search.asp Commented Aug 30, 2017 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. Focus on in_array(), isset(), array_key_exists() and array_keys(). Commented Aug 30, 2017 at 11:17
  • 1
    The duplicate this thread is closed for is incorrect. The linked thread is about Ruby, question is in php. Commented Aug 30, 2017 at 12:11
  • 1
    @Andreas fixed that. Commented Aug 30, 2017 at 12:39

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

3 Comments

This won't work if the key apple exists and is set to null. It's a bit of an edge case, but it might be better to use array_key_exists.
@iainn, ok, edited
In my opinion NULL is a value. If the key does not exist then it's false, but if the key does exist with NULL then the value is true but NULL. YMMV.
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

Comments

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;

Comments

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

Comments

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

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.