1

Hello before I've tried how to merge duplicate value from multidimensional array from the link merge duplicate array values in a multidimensional array php

But that answer just only work for two duplicate value and not work for more than two same value. So if I change this code like this:

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

And I get the result like this:

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
        )

)

And this is not the answer :(, and the results I want is to look like this:

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
        )

)

1 Answer 1

0

You can try with this, but it adds extra key as (test, pass , etc ) but i think this not any problem

<?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>";

Output:

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
                )

        )

)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.