Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a multi-dimensional array say,

Array
(
    [0] => Array
        (
            [id] => 1
            [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
        )

)

Here in the above array, password key has same value in two keys, i need to merge the arrays which have duplicate values to get the following output,

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
        (
            [id] => 3
            [email_id] => [email protected]
            [password] => pass
        )

)

How to do this ? i've tried array_merge & foreach loops, but i can't get this output

share|improve this question
Are the duplicates determined only on the value for index password ? – Teena Thomas Jan 7 at 18:49
yes.. only for password – rxbass Jan 7 at 18:51
For this, you will have to use your own merge function. This you can simply do using a foreach loop, that will determine if two arrays are duplicates, and if they are, make an array with the duplicate – kokx Jan 7 at 18:52
i dnt have two different arrays to merge...single array with multiple key values – rxbass Jan 7 at 19:06

1 Answer

up vote 1 down vote accepted

Try,

$arr = array( array('id'=>1, '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;
  }

  print_r($new_arr);

Demo

share|improve this answer
this works only if keys are next to each other $arr[$k+1] – vodich Jan 7 at 19:16
thank u so much coder1984! its very helpful. but got one notice error like undefined offset 3. – rxbass Jan 7 at 19:37
working fine coder ;) ...thanks again! – rxbass Jan 7 at 19:50
You're welcome. Please accept the answer as it would help someone else too. :) – Teena Thomas Jan 7 at 19:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.