按一列分组行,只有在组中有多行时才创建更深的子数组


Group rows by one column, only create deeper subarrays when multiple rows in group

我有一个像这样的多维数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [email_id] => ok@gmail.com
            [password] => test
        )
    [1] => Array
        (
            [id] => 2
            [email_id] => check@gmail.com
            [password] => test
        )
    [2] => Array
        (
            [id] => 3
            [email_id] => an@gmail.com
            [password] => pass
        )
)

在上面的数组中,password的值在两个不同的行中是相同的。我需要合并具有重复值的数组以获得以下输出:

Array
(
     [0] => Array
            (
               [0] => Array
                (
                    [id] => 1
                    [email_id] => ok@gmail.com
                    [password] => test
                )
        
            [1] => Array
                (
                    [id] => 2
                    [email_id] => check@gmail.com
                    [password] => test
                )
            ) 
    [1] => Array
        (
            [id] => 3
            [email_id] => an@gmail.com
            [password] => pass
        )
)

如何做到这一点?我试过array_merge() & &;foreach()循环,但我不能得到这个输出

试试,

$arr = array( array('id'=>1, 'email_id'=>'ok@gmail.com', 'password'=>'test'),
  array('id'=>2, 'email_id'=>'check@gmail.com', 'password'=>'test'), 
  array('id'=>3, 'email_id'=>'an@gmail.com', 'password'=>'pass'));
  $new_arr = array();
  foreach($arr as $k => $v) {
      if( is_array($arr[$k+1]) && $arr[$k]['password'] === $arr[$k + 1]['password'] )
          $new_arr[] = array($arr[$k], $arr[$k+1]);
      else if( in_array_recursive($arr[$k]['password'], $new_arr) === FALSE ) 
              $new_arr[] = $v;
  }
  function in_array_recursive( $val, $arr) {
      foreach( $arr as $v ) {
          foreach($v as $m) {
              if( in_array($val, $m ) )
                  return TRUE;      
          }
      }
      return FALSE;
  }
  print_r($new_arr);
演示

只有在组中有多个条目时才需要在输出数组中创建更大的深度。我个人不希望在数据结构中出现这种可变性,因为打印数据的代码将需要额外的麻烦来处理可能处于不同级别的关联数据行。

无论如何,这是我如何用一个循环来做的…

Foreach Loop Code: (Demo)

$result = [];
foreach ($array as $row) {
    if (!isset($result[$row['password']])) {
        $result[$row['password']] = $row; // save shallow
    } else {
        if (isset($result[$row['password']]['id'])) {  // if not deep
            $result[$row['password']] = [$result[$row['password']]]; // make original deeper
        }
        $result[$row['password']][] = $row;  // save deep
    }
}
var_export(array_values($result));  // print without temporary keys

功能代码:(Demo)

var_export(
    array_values(
        array_reduce(
            $array,
            function($result, $row) {
                if (!isset($result[$row['password']])) {
                    $result[$row['password']] = $row;
                } else {
                    if (isset($result[$row['password']]['id'])) {
                        $result[$row['password']] = [$result[$row['password']]];
                    }
                    $result[$row['password']][] = $row;
                }
                return $result;
            },
            []
        )
    )
);