Code Snippets: PHP Function array_key_match_pullkey

In between all the other projects I’ve been working on (ads, documentation submissions, etc.) I somehow managed to find the time to work on some PHP scripts for a(n) [internal] web-application that I’m responsible for.

There were multiple times where I realized I was doing the same thing again and again in the code, but for different datasets. I needed to find a function in PHP that simply lets you pass a 2+ dimensional Array, a key for the sub-array by which to search for that matches whatever variable you pass, and pulls the value of a separate key in that same sub-array.

Lets look at an example array:

  $smurf = array();

  $smurf[] = array('id' => 12, name => 'Papa Smurf');
  $smurf[] = array('id' => 56, name => 'Brainy Smurf');
  $smurf[] = array('id' => 59, name => 'Smurfette');

At some point I need to be able to pull a Smurf’s name solely by making use of the ID. However, sometimes I may want to do the exact opposite, or do the same kind of info-pull but with a different Array altogether.

So I came up with this today:

// FUNCTION::array_key_match_pullkey()
// ////////////////////////////////////////////////////
// Parameters:
// 1 (array)  : Array : The array to be searched
// 2 (key)    : String: The key we're looking to match
// 3 (match)  : Wild  : The value of the key to match
// 4 (pullkey): String: The key whose value we want to pull
// ////////////////////////////////////////////////////////////////////
// Returns: FALSE or Wild Variable (any type)
function array_key_match_pullkey($array, $key, $match, $pullkey)
   // Begin to loop through array
   foreach ($array as $index)
      // Look in sub-array for matching key/value
      if ($index[$key] == $match)
         // Match has been found, pull the value of the pullkey
         return $index[$pullkey];
   // No match found - return a FALSE boolean value
   return FALSE;

Obviously it would be simplicity itself to modify this function to work with lower level sub-arrays at multiple dimensions, or whatever. I don’t personally need it to do so, and I diddnt feel like spending more than 5 minutes on writing this function. (I usually spend more time on my comments than my code, hah!)

Anyhow, now it is all too easy to pull a smurf ID’d as #59 and find that its Smurfette (the Stephanie of the Smurfs):

$smurf_name = array_key_match_pullkey($smurfs, ‘id’, 59, ‘name’);

This managed to help me chop down the size of my scripts today, and although it may not be the most optimized method (or simplest even); I couldn’t readily find another function (or array operation) that would let me do the same.

Feel free to let me know if you know of some better method, or think up any modifications.

Belisarius Smith consults as a software engineer, cloud engineer, and security advisor. He has a BSBA in Security Management, a Masters of Software Engineering from Penn State, and is doing graduate studies in Psychology. When he isn't traveling, mountain climbing, or reading, he spends his spare time on personal side projects and studies.

Leave a Reply