Home > Software engineering >  Php grouping values if find duplicate other value key
Php grouping values if find duplicate other value key

Time:06-04

I have this in while loop of my query fetch And i want to regroup values if find occurence in prefix key

while(($arr = $_opDB->fetch_assoc($result)) != FALSE){
        if(!array_search($arr['base_name'], array_column($translate_array, 'connector_prefix'))){
            $translate_array[] = array(
                'prefix' => $arr['base_name'],
                'translate_hostname' => $arr['translate_hostname']
            );
        } else {
            $translate_array[$i-1]['translate_hostname'][] = $arr['translate_hostname'];
        }

        $i  ;
}
...

Array
(
    ...

    [3] => Array
        (
            [prefix] => DD
            [translate_hostname] => dd
        )

    [4] => Array
        (
            [prefix] => DD
            [translate_hostname] => de
        )

)

If prefix is same that previous loop so add in array to previous 'translate_hostname' like that

[3] => Array
        (
            [prefix] => DD
            [translate_hostname] => 
              [0] => Array
                      (
                          [translate] => dd
                      )

              [1] => Array
                      (
                          [translate] => de
                      )

        )

But it doesn't work, thanks for help

CodePudding user response:

if I understand right you need to keep the same records that have the same base_name at the same row with converting the key of translate_hostname to an array with both values

to do that we can

1-loop on all records

2-group records with base_name by using isset() to check wither its the first record that has this basename or add this record to the group

3-if it's not the first record check if the translate_hostname that exists is array or not if not convert it to an array and add to it the first value and the current looping row value

code:-

while(($arr = $_opDB->fetch_assoc($result)) != FALSE){
    
    if(!isset($translate_array[$arr['base_name']]))
    {
        $translate_array[$arr['base_name']] = array(
                'prefix' => $arr['base_name'],
                'translate_hostname' => $arr['translate_hostname']
            );
            
    }else {
        
        $translate_hotnames = $translate_array[$arr['base_name']]["translate_hostname"];
                
        if(!is_array($translate_hotnames)){
            
            $translate_hotnames = [$translate_hotnames];
            $translate_hotnames[] = $arr['translate_hostname'];
        }
        else {
            $translate_hotnames[] = $arr['translate_hostname'];
        }
        
        $translate_array[$arr['base_name']]["translate_hostname"] = $translate_hotnames;
    }

}

this will lead to the following result "associative" array grouping :

array(1) {
  ["DD"]=>
  array(2) {
    ["prefix"]=>
    string(2) "DD"
    ["translate_hostname"]=>
    array(2) {
      [0]=>
      string(2) "dd"
      [1]=>
      string(2) "de"
    }
  }
}

to retrieve it as mentioned result "indexed" array you can use array_values()

by adding the following line after the loop

$translate_array = array_values($translate_array)

the result will be:-

array(1) {
  [0]=>
  array(2) {
    ["prefix"]=>
    string(2) "DD"
    ["translate_hostname"]=>
    array(2) {
      [0]=>
      string(2) "dd"
      [1]=>
      string(2) "de"
    }
 

} }

  • Related