Last active
          March 16, 2018 11:15 
        
      - 
      
- 
        Save JakubTesarek/d026f60d4baaef810228 to your computer and use it in GitHub Desktop. 
    Interview question for PHP developers
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| /* | |
| Objectives: | |
| - Create PHP script that will translate input data to expected output from example below. | |
| - Calculate time complexity of your script | |
| bonus: Implement solution that will not use pass-by-reference and will not use objects | |
| */ | |
| $input = [ | |
| 'A' => 1, | |
| 'B_C' => 2, | |
| 'B_D' => 3, | |
| 'E_F_G' => 4, | |
| 'E_H' => 5 | |
| ]; | |
| $expectedOutput = [ | |
| 'A' => 1, | |
| 'B' => [ | |
| 'C' => 2, | |
| 'D' => 3 | |
| ], | |
| 'E' => [ | |
| 'F' => [ | |
| 'G' => 4 | |
| ], | |
| 'H' => 5 | |
| ] | |
| ]; | 
        
      
            dg
  
      
      
      commented 
        Aug 15, 2014 
      
    
  
$output = [];
function parseEl($k, $v) {
    return [$k[0] => isset($k[2]) ? parseEl(substr($k, 2), $v) : $v];
}
foreach ($input as $k => $v) {
    $output = array_merge_recursive($output, parseEl($k, $v));
}
foreach($input as $k => $v)
    eval("\$output['".str_replace('_',"']['",$k)."']=$v;");OK, didn't want to post it here initially, but for the record ;)
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment