0

Let's say if I have this variable $a = 'test';

Now let's say I created an array of multiple items like so:

$array['hello'] = 'yes';
$array['goodbye'] = 'no';
$array['afternoon'] = 'test';

Is it possible to check if any of the items in the array are the same, similar to isset(), then to get which item it is equal to it ($array['afternoon'])?

3
  • Seems like you're after array_search: stackoverflow.com/questions/6046908/php-index-of-item Commented Sep 15, 2019 at 7:59
  • You can check array has duplicated by this code: ` function array_has_dupes($array) { return count($array) !== count(array_unique($array)); }` Commented Sep 15, 2019 at 8:01
  • Thank you that is actually what I was looking for Nick. I didn't know that the function existed. Commented Sep 15, 2019 at 8:01

1 Answer 1

0

just use foreach()

foreach($array as $key => $val){
 if($key == $a){
  echo $val;
 }
}

Better way

use array_key_exists()

if(array_key_exists($a, $array)){
 echo $a;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry I was in the middle of fixing it to how I need it, but I have updated it.
I updated my answer, and I didn't realize you were trying to check for duplicate value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.