-1

Hi please help me to do find duplicate values and combine values in an array. i checked with the

How to remove duplicate values from an array in PHP

array_map("unserialize",array_unique(array_map("serialize", $outer_array)));

but this is not actually I need . my requirement is

I have an array like this

INPUT

 Array(
 [0] => Array
        (
            [id] => 1
            [latitude] => 12.9614126
            [longitude] => 77.5610838
            [type] => signature
        )

    [1] => Array
        (
            [id] => 2
            [latitude] => 12.9614126
            [longitude] => 77.5610838
            [type] => customer
        )

    [2] => Array
        (
            [id] => 2
            [latitude] => 12.9614126
            [longitude] => 77.5610838
            [type] => signature
        )
     [3] => Array
        (
            [id] => 2
            [latitude] => 12.9614126
            [longitude] => 77.5610838
            [type] => signature
        )
)

In the above array the key 1 and 2 contains same value for (id,latitude,longitude) but it different for type. and one more thing is the type contain duplicated that also need to remove

Here my expected result

OUTPUT

Array(
     [0] => Array
            (
                [id] => 1
                [latitude] => 12.9614126
                [longitude] => 77.5610838
                [type] => signature
            )

        [1] => Array
            (
                [id] => 2
                [latitude] => 12.9614126
                [longitude] => 77.5610838
                Array
               (
                  [0]=> Array(
                        [type] => customer
                      )
                   [1]=> Array(
                        [type] => signature
                      )
                )
            )
    )

I tried this

$outer_array = array();
$unique_array = array();
foreach($arraydata as $image){
   $inner_array = array();
   $fid_value = $image['id'];
   if(!in_array($image['id'], $unique_array))
   {
   array_push($unique_array, $fid_value);
   array_push($inner_array, $image);
   $outer_array[$fid_value] = $inner_array;
}else{
  array_push($outer_array[$fid_value], $image);
}
}
Vinai Raj
  • 151
  • 2
  • 15
  • Is it must for all of these values to be similar (id,latitude,longitude) what if it was only the id !? And if you have a similar id does that mean you have similar longitude and latitude !? It's not obvious coz all of them have the same values ! – Mohammad Tbeishat Sep 21 '18 at 11:24
  • @Vinai Raj, Did you check my answer, I hope this will give you the expected result. – Gajanan Kolpuke Sep 21 '18 at 11:31

1 Answers1

0

Plz refer below code for your expected result

$arrUnique = [];
$arrResult = [];
$arrUniqueValue = [];
foreach ( $arraydata as $value ) {
    $intID = $value['id'];
    if( in_array( $intID, $arrUnique ) ) {
        foreach ( $value as $k => $v ) {
            if( $arrResult[$intID][$k] == $v )
                continue;
            $arrUniqueValue[$intID][$k][] = $v;
            $arrResult[$intID][$k] = $arrUniqueValue;
        }
    } else {
        array_push( $arrUnique, $intID );
        $arrResult[$intID] = $value;
    }
}

I hope this will solve your problem.

Gajanan Kolpuke
  • 155
  • 1
  • 1
  • 14