Kevin.a Kevin.a - 5 days ago 5
PHP Question

Matching a string with an array key

I'm trying how to figure something out for my website.
This is not my code just an example to make it easier to understand what i mean.

Lets say i have an array like this:

$services = array(
Marketing => marketing:342343423423,
Sales => sales:779876786,
)


And i have a form on my website. I can get the posted values with a POST request.

The POST request can for example look like this

$_POST['service_request']


Now what i want to know is how to do the next:

if
$_POST['service_request']
matches one of the array keys inside
$services
then print the relevant value of this array key.

So lets say an user fills my form, and his service request is marketing then i want to check if this service request exists inside the
$service
variable and if it exist print the value.

Answer

use key_exists function of php.

 if(key_exists($_POST['service_request'],$services)){
   //exists, perform rest of the logic here.
 }