-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;
    }
3
  • Based on your example: $result['project'][$source['proj']['general']['keys'][1]] = 10; Commented Feb 27, 2018 at 17:35
  • This is less simple than you might think. You basically need a recursive function. Show us what you've attempted so far. Commented Feb 27, 2018 at 17:38
  • 1
    ['app_name'], 'type' => 'int'], => API sample Is that error? Commented Feb 27, 2018 at 19:50

1 Answer 1

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].
Sign up to request clarification or add additional context in comments.

3 Comments

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).
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.