1

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

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()循环,但我无法得到这个输出。

4

2 回答 2

2

尝试,

$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);

演示

于 2013-01-07T19:11:40.620 回答
-1

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

无论如何,这就是我如何用一个循环来做到这一点......

Foreach 循环代码:(演示

$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

功能代码:(演示

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;
            },
            []
        )
    )
);
于 2021-08-22T20:59:07.033 回答