1

This is my array. And i need to concat array values if duplicate exists.

Array
(
    [0] => Array
        (

            [id] => 3
            [location_id] => 2            
            [location_name] => 1st Floor 
            [type] => 1           
        )

    [1] => Array
        (

            [id] => 6
            [location_id] => 2           
            [location_name] => 1st Floor 
            [type] => 1           
        )

    [2] => Array
        (           
            [id] => 7
            [location_id] => 1            
            [location_name] => Ground Floor 
            [type] => 1        
        )

)

And below is my code, which doesn't concat on unique values.

$conct= array();
foreach ($myArray as $array)
{
    foreach ($array as $key => $value)
    {               
        if ( ! isset($merged[$key]))
        {
            $conct[$key] = $value;
        }
        else
        {
            $conct[$key] .= ",".$value;
        }
    }
}

This is giving me.

Array
(
    [0] => Array
        (

            [id] => 3,6,7
            [location_id] => 2,2,1            
            [location_name] => 1st Floor,1st Floor,Ground Floor
            [type] => 1,1,1       
        )


)

And i need to concat the values based on unique location_id and location_name. My result array should be.

Array
(
    [0] => Array
        (

            [id] => 3,6
            [location_id] => 2,2            
            [location_name] => 1st Floor,1st Floor 
            [type] => 1,1           
        )

    [1] => Array
        (

            [id] => 7
            [location_id] => 1           
            [location_name] => Ground Floor 
            [type] => 1           
        )


)

How to achieve this?

2 Answers 2

2

Try this..

<?php

$new_values = array();
$values = array(
    array('id'=> 1, 'location_id' => 2, 'location_name' => '1st Floor','type'=>1),
    array('id'=> 6, 'location_id' => 2, 'location_name' => '1st Floor','type'=>1),
   array('id'=> 7, 'location_id' => 1, 'location_name' => 'Gound Floor','type'=>1),
);

foreach($values as $value) {
    if(isset($new_values[$value['location_id']])) {
        $temp = $new_values[$value['location_id']];
        $temp['id'] .= ',' . $value['id'];
        $temp['location_id'] .= ',' . $value['location_id'];
         $temp['location_name'] .= ',' . $value['location_name'];
          $temp['type'] .= ',' . $value['type'];
        $new_values[$value['location_id']] = $temp;
    } else {
        $new_values[$value['location_id']] = $value;
    }
}

$new_values = array_values($new_values);
print_r($new_values);
?>

Output:Array ( [0] => Array ( [id] => 1,6 [location_id] => 2,2 [location_name] => 1st Floor,1st Floor [type] => 1,1 ) [1] => Array ( [id] => 7 [location_id] => 1 [location_name] => Gound Floor [type] => 1 ) )

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

Comments

2
$input  = array(array('id'=>3,'location_id'=>2,'location_name'=>'1st Floor','type'=>1),
                array('id'=>6,'location_id'=>2,'location_name'=>'1st Floor','type'=>1),
                array('id'=>7,'location_id'=>1,'location_name'=>'Ground Floor','type'=>1)
            );
$conct = array();
foreach($input as $k => $_input) {
    foreach($_input as $key => $value) {
        if(isset($conct[$key])) {
            if(check_duplicate($duplicate,$input[$k])) {
                $conct[$key] .= ",".$value;    
            } else {
                $new[$key]  = $value;
            }
        }
        else
        {
            $conct[$key] = $value;
            if($key=='location_id'||$key=='location_name') 
                $duplicate[$key] = $value;
        }
    }

}

function check_duplicate($duplicate=array(),$input=array()) {

    foreach($duplicate as $dupe) {
        if($dupe===$input['location_id'] || $dupe === $input['location_name'] )
            return true;
        else
            return false; 
    }

}
echo "<pre>"; print_r($conct);
echo "<pre>"; print_r($new);

1 Comment

Answer Array ( [id] => 3,6 [location_id] => 2,2 [location_name] => 1st Floor,1st Floor [type] => 1,1 ) Array ( [id] => 7 [location_id] => 1 [location_name] => Ground Floor [type] => 1 )

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.