2015-03-31 14 views
1

你好之前,我已經試過了如何從多維數組從鏈接merge duplicate array values in a multidimensional array phpPHP的多維數組PHP的

但是這個答案合併重複的值合併重複的數組值僅僅只有兩個重複的值工作,而不是更多的工作比兩個相同的價值。所以,如果我改變了這樣的代碼:

$arr = array(array('id'=>1, 'email_id'=>'[email protected]', 'password'=>'test'), 
    array('id'=>2, 'email_id'=>'[email protected]', 'password'=>'test'), 
    array('id'=>2, 'email_id'=>'[email protected]', 'password'=>'test'), 
    array('id'=>3, 'email_id'=>'[email protected]', '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] => [email protected] 
        [password] => test 
       ) 

      [1] => Array 
       (
        [id] => 2 
        [email_id] => [email protected] 
        [password] => test 
       ) 

     ) 

    [1] => Array 
     (
      [0] => Array 
       (
        [id] => 2 
        [email_id] => [email protected] 
        [password] => test 
       ) 

      [1] => Array 
       (
        [id] => 2 
        [email_id] => [email protected] 
        [password] => test 
       ) 

     ) 

    [2] => Array 
     (
      [id] => 3 
      [email_id] => [email protected] 
      [password] => pass 
     ) 

) 

這是沒有答案:(,和我想要的結果是這個樣子:

Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [id] => 1 
        [email_id] => [email protected] 
        [password] => test 
       ) 

      [1] => Array 
       (
        [id] => 2 
        [email_id] => [email protected] 
        [password] => test 
       ) 

      [2] => Array 
       (
        [id] => 2 
        [email_id] => [email protected] 
        [password] => test 
       ) 
     ) 

    [1] => Array 
     (
      [id] => 3 
      [email_id] => [email protected] 
      [password] => pass 
     ) 

) 

回答

0

你可以用這個嘗試,但它增加了額外的鍵(試驗,通過等),但我認爲這不是什麼問題

<?php 
$arr = array(array('id'=>1, 'email_id'=>'[email protected]', 'password'=>'test'), 
array('id'=>2, 'email_id'=>'[email protected]', 'password'=>'test'), 
array('id'=>2, 'email_id'=>'[email protected]', 'password'=>'test'), 
array('id'=>3, 'email_id'=>'[email protected]', '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] => [email protected] 
        [password] => test 
       ) 

      [1] => Array 
       (
        [id] => 2 
        [email_id] => [email protected] 
        [password] => test 
       ) 

      [2] => Array 
       (
        [id] => 2 
        [email_id] => [email protected] 
        [password] => test 
       ) 

     ) 

    [pass] => Array 
     (
      [0] => Array 
       (
        [id] => 3 
        [email_id] => [email protected] 
        [password] => pass 
       ) 

     ) 

)