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 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题