0

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

  • This will be easier if you use `$arr['base_name']` as the key in your `$translate_array` instead of using numeric keys. If you need numeric keys in the end, you can use `array_values` to reindex after you finish the grouping. – Don't Panic Jun 03 '22 at 16:32
  • See three unique and concise (conditionless) iterative techniques [here](https://stackoverflow.com/a/72497398/2943403). To implement the classic `foreach()` for this question, see this [demo](https://3v4l.org/8ONOF). The approaches in the dupe target are superior to the answer from Omar. – mickmackusa Jun 04 '22 at 05:57

1 Answers1

1

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"
    }
 

} }

Omar Tammam
  • 1,217
  • 1
  • 9
  • 17