Php合并多维数组php中的重复数组值

时间:2015-03-31 13:09:12

标签: php arrays multidimensional-array merge

您好我之前尝试过如何从链接merge duplicate array values in a multidimensional array php

合并多维数组中的重复值

但是这个答案只适用于两个重复的值而不适用于两个以上相同的值。所以,如果我改变这样的代码:

$arr = array( array('id'=>1, 'email_id'=>'ok@gmail.com', 'password'=>'test'),
  array('id'=>2, 'email_id'=>'check@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;
  }

echo "<pre>";
print_r($arr);
echo "</pre>";

echo "<pre>";
print_r($new_arr);
echo "</pre>";

我得到的结果如下:

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
        (
            [0] => Array
                (
                    [id] => 2
                    [email_id] => check@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
        )

)

这不是答案:(,我想要的结果是这样的:

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
                )

            [2] => Array
                (
                    [id] => 2
                    [email_id] => check@gmail.com
                    [password] => test
                )
        )

    [1] => Array
        (
            [id] => 3
            [email_id] => an@gmail.com
            [password] => pass
        )

)

1 个答案:

答案 0 :(得分:0)

你可以试试这个,但它增加了额外的密钥(测试,传递等),但我认为这没有任何问题

<?php
$arr = array( array('id'=>1, 'email_id'=>'ok@gmail.com', 'password'=>'test'),
array('id'=>2, 'email_id'=>'check@gmail.com', 'password'=>'test'), 
array('id'=>2, 'email_id'=>'check@gmail.com', 'password'=>'test'), 
array('id'=>3, 'email_id'=>'an@gmail.com', 'password'=>'pass'));


echo "<pre>";
print_r($arr);



foreach($arr as $k => $v) {
    $new_arr[$v['password']][]=$v;
}
print_r($new_arr);
echo "</pre>";

输出

Array
(
    [test] => 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] => 2
                    [email_id] => check@gmail.com
                    [password] => test
                )

        )

    [pass] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [email_id] => an@gmail.com
                    [password] => pass
                )

        )

)