-1

I am really getting stuck in renaming an array using a mapping array in PHP. I am able to pick and change the name of elements which has string or int content. But really getting messed up with a nested array.

Mapping array:

$source = array(
    'id'   =>   ['keys' => ['app_id'], 'type' => 'int'],
    'name' =>   ['keys' => ['app_name'], 'type' => 'string'],
    'proj' =>   [
        'general'   => ['keys' => ['project', 'gen'], 'type' => 'string'],
        'category'  => ['keys' => ['project', 'cat'], 'type' => 'string']
    ]
);

Before transforming:

$post = array(
    'id' => 1000,
    'name' => 'API sample',
    'proj' => array(
        'general'  => 10,
        'category' => 50
    ) 
);

Desired output:

$result = array(
    'app_id' => 1000,
    'app_name' => 'API sample',
    'project' => array(
        'gen'  => 10,
        'cat' => 50
    ) 
);

Code:

   function renameArray($mappings,$inputObjectDb) {
        $inputObjectDb = (array)$inputObjectDb;
        $response = [];
        foreach($mappings as $key => $mapping) {
            //call itself in order to prepare sub array
            if(!isset($mapping['keys'])) {
                $response[$key] = prepareOutputData($mapping,$inputObjectDb);
            } else {
                $dbSubarray = $inputObjectDb;
                foreach( $mapping['keys'] as $subkey) {
                    $response[$key] = $dbSubarray[$subkey];
                    $dbSubarray = &$dbSubarray[$subkey];
                }
            }

        }
        return $response;
    }
Bastin Robin
  • 907
  • 16
  • 30

1 Answers1

0

Try this:

foreach ($source as $k=>$v) {
        if(!is_array($post[$k]))
                $ret[$v['keys'][0]] = $post[$k];
        else {
                foreach($v as $kk=>$vv)
                        $ret[$vv['keys'][0]][$vv['keys'][1]] = $post[$k][$kk];
        }
}

print_r($ret);

The final output of this will be stored in $ret.

Variables that I'll use bellow:

  • $k - will refer to the key of $source in the current iteration
  • $v - will refer to the array associated at $source[$k]
  • $kk - will refer to the key of $v in the current iteration
  • $vv - will refer to the array associated at $v[$kk]

Firstly, we loop over the elements of $source. Inside we have two options:

  • $post[$k] holds value(not array). In this case, we build an element for $ret. The key will use $v['keys'][0](the first element of the keys array in $v). The value will use the data from $post[$k].
  • $post[$k] is an array. In this case we will iterate over $v. In each iteration we build an array element for $ret. The array key will be $vv['keys'][0](the first element of the keys array of $vv. The element of $vv['keys'][0] will have a key that will be $vv['keys'][1](the second element of keys array of $vv). The value for $vv['keys'][1] will be $post[$k][$kk].
man0v
  • 654
  • 3
  • 13
  • None of the variable names are all that useful. Descriptive variable names are better. Rather than `$k => $v`, why not `$key => $attributes`, and instead of `$kk => $vv`, `$sub_key => $sub_attributes` (just for example). – random_user_name Feb 27 '18 at 19:04
  • 1
    What is a useful variable name? _The best code communicates itself_: https://medium.com/mindorks/meaningful-names-a-dimension-of-writing-clean-code-fdae1ae4f0b1 and http://www.makinggoodsoftware.com/2009/05/04/71-tips-for-naming-variables/ and https://stackoverflow.com/questions/203618/how-to-name-variables – random_user_name Feb 27 '18 at 19:47
  • 1
    **I am just trying to help you** be a better coder. Do what you want with the tips. I've learned a LOT from other developers on this site. If you are willing to listen / receive input, you can too. Or, you can be a smart-aleck and reject the feedback that other developers offer. Up to you. – random_user_name Feb 27 '18 at 20:09