Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to remove Keys from a 2D Array?

I have an Array that looks like this:

array(
  0 => array(
    'key1' => 'a',
    'key2' => 'b',
    'key3' => 'c'
  ),
  1 => array(
    'key1' => 'c',
    'key2' => 'b',
    'key3' => 'a'
  ),
  ...
)

I need a function to get an array containing just a (variable) number of keys, i.e. reduce_array(array('key1', 'key3')); should return:

array(
  0 => array(
    'key1' => 'a',
    'key3' => 'c'
  ),
  1 => array(
    'key1' => 'c',
    'key3' => 'a'
  ),
  ...
)

What is the easiest way to do this? If possible without any additional helper function like array_filter or array_map as my coworkers already complain about me using too many functions.

The source array will always have the given keys so it's not required to check for existance.

Bonus points if the values are unique (the keys will always be related to each other, meaning that if key1 has value a then the other key(s) will always have value b).

My current solution which works but is quite clumsy (even the name is horrible but can't find a better one):

function get_unique_values_from_array_by_keys(array $array, array $keys)
{
        $result = array();
        $found = array();

        if (count($keys) > 0)
        {
                foreach ($array as $item)
                {
                        if (in_array($item[$keys[0]], $found)) continue;
                        array_push($found, $item[$keys[0]]);
                        $result_item = array();
                        foreach ($keys as $key)
                        {
                                $result_item[$key] = $item[$key];
                        }
                        array_push($result, $result_item);
                }
        }
        return $result;
}

Addition:
PHP Version is 5.1.6.

like image 854
Morfildur Avatar asked Apr 06 '10 12:04

Morfildur


2 Answers

If your coworkers don't like array_filter or array_map, that is their lack of education and taste. While I don't advise burning bridges at work, they frequently are the best way to solve a problem.

function reduce_array($array, $keys) {
  $keys_map = array_fill_keys($keys, true);
  return array_map(function ($subarray) use ($keys) {
    return array_intersect_key($subarray, $keys_map);
  }, $array);
}

(Note: the anonymous functions I use above are only available in PHP >= 5.3, so you probably can't use them)

If you must do it without array_map and anonymous functions:

function reduce_array($array, $keys) {
  $ret = array();
  $keys_map = array_fill_keys($keys, true);
  foreach ($array as $subarray) {
    array_push($ret, array_intersect_key($subarray, $keys_map));
  }
  return $ret;
}

To briefly explain: first, I use array_fill_keys to turn keys from an integer-indexed array to a string-indexed array (the value I give it, true, doesn't really matter here). This makes it suitable input for array_intersect_key, which will do the exact reduction you're looking for.

like image 153
jemfinch Avatar answered Oct 16 '22 09:10

jemfinch


Only using in_array() function. Character count can be reduced but we'll leave it readable:

function reduce_array($array, $keys) {
    $res=array(); // Our result array
    foreach($array as $v1){ // Loop thru original array
        $t1=array(); // Our new inner array
        foreach($v1 as $k2=>$v2){ // Loop thru original inner array
            if(in_array($k2,$keys)) $t1[$k2]=$v2; //If inside inner array store
        }
        $res[]=$t1; //Add to result array
    }
    return($res);
}

I'm itching to get rid of the in_array() function as well but I really must do some work ;)

like image 21
zaf Avatar answered Oct 16 '22 10:10

zaf