Search code examples
phparraysmultidimensional-arraymergeunique

find duplicate value in multidimensional array and combine their ID in comma separated with accordingly using PHP


I have multidimensional array which listed in dynamically call from the database where need to have apply filter on conditional base of unique value and their ID merge with comma separated accordingly as with below desired result.

$arr = array();

$arr[0]['attribute'] = 'A';
$arr[0]['value'] = array('tenghao','Yuncheng','TAIFENG','Xinsheng','Laiwu Wanxin');
$arr[0]['ID'] = array(3561,3568,3560,3567,3569);

$arr[1]['attribute'] = 'B';
$arr[1]['value'] = array('Red, Red','Red','Red','Red','Red', 'Red');    
$arr[1]['ID'] = array(3567,3569,3561,3568,3569,3560);

$arr[2]['attribute'] = 'C';
$arr[2]['value'] = array("90%","80%","80%-100%","90%","100%");    
$arr[2]['ID'] = array(3567,3569,3561,3568,3560);

Desired Result

Array
(
    [0] => Array
        (
            [attribute] => A
            [value] => Array
                (
                    [0] => tenghao
                    [1] => Yuncheng
                    [2] => TAIFENG
                    [3] => Xinsheng
                    [4] => Laiwu Wanxin
                )

            [ID] => Array
                (
                    [0] => 3561
                    [1] => 3568
                    [2] => 3560
                    [3] => 3567
                    [4] => 3569
                )

        )

    [1] => Array
        (
            [attribute] => B
            [value] => Array
                (
                    [0] => Red, Red
                    [1] => Red
                )

            [ID] => Array
                (
                    [0] => 3567
                    [1] => 3569,3561,3568,3569,3560
                )

        )

    [2] => Array
        (
            [attribute] => C
            [value] => Array
                (
                    [0] => 90%
                    [1] => 80%
                    [2] => 80%-100%
                    [3] => 100%
                )

            [ID] => Array
                (
                    [0] => 3567,3568
                    [1] => 3569
                    [2] => 3561
                    [3] => 3560
                )

        )

)

Solution

  • You can use next code for getting your result:

    $ar_res  = []; 
    
    foreach($arr as $ind => $row){  
    
        $vals = [];    // array with unique values
        $ids = [];     // array with joined ids
    
        $ar_res[$ind]['attribute'] = $row['attribute'];     // copy attribute field
    
        foreach($row['value'] as $key => $val){
    
            // analysing each value of each row  
    
            if (in_array($val,$vals)){   
            // if it's an old value
            // then join corresponded ID
    
                $ids[array_search($val, $vals)] .= ', '.$row['ID'][$key];
            } else { 
                // if this is a new value 
                // then add corresponded ID with value in both arrays 
    
                $vals[] = $val;
                $ids[] = $row['ID'][$key];  
            } 
        }
        // resultant array
        $ar_res[$ind]['value'] = $vals;
        $ar_res[$ind]['ID'] = $ids;
    } 
    

    Demo