douzi8112 2010-04-06 12:53
浏览 27
已采纳

从2D阵列中删除键的最简单方法是什么?

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.

  • 写回答

2条回答 默认 最新

  • douhuang9886 2010-04-06 13:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)