0

I am trying to merge the multiple arrays into single arrays and making the values unique.

$array = array ( 
        $array1 = array(
        "id" => 205,
        "firstName" => "NANCY GEORGE",
        "gender" => "F",
        "age" => 21,
        "alert" => "bmi"
    ),
    $array2 = array(
        "id" => 205,
        "firstName" => "NANCY GEORGE",
        "gender" => "F",
        "age" => 21,
        "alert" => "bp"
    ),
    $array3 = array(
        "id" => 206,
        "firstName" => "KUTAPPA.A.C",
        "gender" => "M",
        "age" => 31,
        "alert" => "bmi"
    )
    );  

And here is my expected output,

$array = array ( 
        '205' = array(
        "id" => 205,
        "firstName" => "NANCY GEORGE",
        "gender" => "F",
        "age" => 21,
        "alert" => "bmi,bp"
    ),
    '206' = array(
        "id" => 206,
        "firstName" => "KUTAPPA.A.C",
        "gender" => "M",
        "age" => 31,
        "alert" => "bmi"
    )
);

I am trying to merge the array in foreach, to create the new array, but its not working to get the expected result.

5
  • 1
    "it's not working" what isn't working? You haven't posted any code. Loop through your original array and check if a key already exists in your new array with the same ID, if it exists, add a value to alert, if not add an item to the array. Commented Mar 8, 2016 at 12:35
  • Possible duplicate of How do I use array_unique on an array of arrays? Commented Mar 8, 2016 at 12:36
  • Search before ask. Stackoverflow is full of questions and answers like this Commented Mar 8, 2016 at 12:36
  • 1
    ..perhaps the more important question is why do you have this data with all of these duplicates? Why can't you just use GROUP_CONCAT if you get this data from a database? Commented Mar 8, 2016 at 12:37
  • The expected output you posted is not valid PHP code. The second line should probably read '205' => array(. The same for the line '206' = array(. Commented Mar 8, 2016 at 13:42

3 Answers 3

1

This might help you.

  $arr1 = array(
        "id" => 205,
        "firstName" => "NANCY GEORGE",
        "gender" => "F",
        "age" => 21,
        "alert" => "bmi"
    );
    $arr2 = array(
        "id" => 205,
        "firstName" => "NANCY GEORGE",
        "gender" => "F",
        "age" => 21,
        "alert" => "bp"
    );
    $arr3 = array(
        "id" => 206,
        "firstName" => "KUTAPPA.A.C",
        "gender" => "M",
        "age" => 31,
        "alert" => "bmi"
    );
    $arr4 = array($arr1, $arr2, $arr3);


    $n = count($arr4);

    for($i=0; $i<=$n; $i++){
        if($arr4[$i]['id'] == $arr4[$i+1]['id']){
            $id = $arr4[$i]['id'];
            foreach($arr4[$i] as $key=>$value){
                if($arr4[$i][$key] <> $arr4[$i+1][$key]){
                    $str = array($arr4[$i][$key], $arr4[$i+1][$key]);
                    $str = implode(',', $str);
                    $arr5[$id][$key] = $str;
                }
                else {
                    $arr5[$id][$key] = $arr4[$i][$key];
                }
            }
            $i++;
        }
        elseif($arr4[$i]['id'] <> $arr4[$i+1]['id']) {
            $id = $arr4[$i]['id'];
            $arr5[$id] = $arr4[$i];
        }

    }

    print_r($arr5);

Output-

Array
(
    [205] => Array
        (
            [id] => 205
            [firstName] => NANCY GEORGE
            [gender] => F
            [age] => 21
            [alert] => bmi,bp
        )

    [206] => Array
        (
            [id] => 206
            [firstName] => KUTAPPA.A.C
            [gender] => M
            [age] => 31
            [alert] => bmi
        )

)
Sign up to request clarification or add additional context in comments.

Comments

1

This should do the trick but as one of the comments pointed out if you can fix the database to group these for you that would be ideal.

<?php
$arrays = array ( 
$array1 = array(
  "id" => 205,
    "firstName" => "NANCY GEORGE",
  "gender" => "F",
  "age" => 21,
  "alert" => "bmi"
),
$array2 = array(
  "id" => 205,
  "firstName" => "NANCY GEORGE",
  "gender" => "F",
  "age" => 21,
  "alert" => "bp"
),
$array3 = array(
  "id" => 206,
  "firstName" => "KUTAPPA.A.C",
  "gender" => "M",
  "age" => 31,
  "alert" => "bmi"
)
);

$cleanArray; //???
foreach($arrays as $array){
if(!empty($cleanArray)){
    foreach($cleanArray as $cleanKey => $clean){
        if($array['id'] == $cleanKey){
            //Just did alerts because I hope nothing else needs this...
            if(!($array['alert'] == $clean['alert'])){
                $cleanArray[$cleanKey]['alert'] .= ", " . $array['alert'];
            }
        } else {
            $id = $array['id'];
            unset($array['id']);
            $cleanArray[$id] = $array;
        }
    }
} else {
    $id = $array['id'];
    unset($array['id']);
    $cleanArray[$id] = $array;
}
}
echo"<pre>";
print_r($cleanArray);

Comments

0

Try will work for you...

   <?php 
   $array = array ( 
   $array1 = array(
        "id" => 205,
        "firstName" => "NANCY GEORGE",
        "gender" => "F",
        "age" => 21,
        "alert" => "bmi"
    ),
    $array2 = array(
        "id" => 205,
        "firstName" => "NANCY GEORGE",
        "gender" => "F",
        "age" => 21,
        "alert" => "bp"
    ),
    $array3 = array(
        "id" => 206,
        "firstName" => "KUTAPPA.A.C",
        "gender" => "M",
        "age" => 31,
        "alert" => "bmi"
    )
);
echo "<pre>"; print_r($array);

$newArray =array();  
foreach($array as $key => $val)
{  
    $newArray[$val['id']] = $val;
}
echo "<pre>"; print_r($newArray);

?>

Output

Array
(
    [205] => Array
        (
            [id] => 205
            [firstName] => NANCY GEORGE
            [gender] => F
            [age] => 21
            [alert] => bp
        )

    [206] => Array
        (
            [id] => 206
            [firstName] => KUTAPPA.A.C
            [gender] => M
            [age] => 31
            [alert] => bmi
        )

)

Comments

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.