daGrevis
daGrevis

Reputation: 21333

Function that checks for keys in array and return array with those keys

I remember that there were function that have such functionality. Unfortunately, I don't remember the name of it. Basically, it did something like...

$values = foo(array('x', 'y', 'z'), $_POST);

If there are such keys in the array, it does return new array (named $values) with only those keys... taken from $_POST. If one or more keys aren't in $_POST, it simply returns false.

Anyone remember something like that or I was just dreaming? Thanks in advice!

Upvotes: 0

Views: 52

Answers (3)

Sumair Zafar
Sumair Zafar

Reputation: 98

check out this function array_key_exists

<?php

   $search_array = array('first' => 1, 'second' => 4);
   if (array_key_exists('first', $search_array)) 
   {
     echo "The 'first' element is in the array";
   }
?>

http://www.php.net/manual/en/function.array-key-exists.php

Upvotes: 0

vaidas
vaidas

Reputation: 494

I think the function you are looking for is array_intersect_key() As of PHP 5.1.0.

array array_intersect_key ( array $array1 , array $array2 [, array $ ... ] )

Parameters

array1 - The array with master keys to check.

array2 - An array to compare keys against.

array - A variable list of arrays to compare.

Returns an associative array containing all the entries of array1 which have keys that are present in all arguments.

Upvotes: 0

Related Questions