dongshan3806 2014-12-09 16:14 采纳率: 0%
浏览 27
已采纳

设置多维数组

I need to format a flat array into a multidimensional array following client's template. Here is my flat array :

$client = array(
    'LastName'        => 'DUPOND',
    'FirstName'       => 'JEAN',
    'Email'           => 'jdupond@free.fr',
    'Address1'        => '126 QUAI BACALAN',
    'ZipCode'         => '33160',
    'City'            => 'BORDEAUX',
    'Country'         => 'FR'
);

And here is my client's template :

    $Template = array(
    'Header'       => array(
        'Context'      => array(
                'LastName'     => '',
                'FirstName'    => ''
        ),
        'Localization' => array(
            'ZipCode'      => '',
            'City'         => '',
            'Country'      => '',
        ),
        'Address1'     => '',
    ),
    'Options'      => array(
        'Email'        => '',
    ),
);

So in the end i need to have a array that looks like :

$Template = array(
'Header'       => array(
    'Context'      => array(
            'LastName'     => 'DUPOND',
            'FirstName'    => 'JEAN'
    ),
    'Localization' => array(
        'ZipCode'      => '33160',
        'City'         => 'BORDEAUX',
        'Country'      => 'FR',
    ),
    'Address1'     => '126 QUAI BACALAN',
),
'Options'      => array(
    'Email'        => 'jdupond@free.fr',
));

But it must work with any kind of template. It should work even if tomorrow clients decide to change template structure.

So from a day to another, the template can suddently become :

$Template = array(
'Header' => array(
    'LastName' => '',
    'FirstName' => '',
    'Localization' => array(
        'ZipCode' => '',
        'Deeper' => array(
            'EvenDeeper' => array(
                'Deepest' => array(
                    'City' => '',
                    'Country' => ''
                )
            )
        )
    )
),
'Options' => array(
    'Email' => '',
    'Address1' => ''
));

and the function will still work !

  • 写回答

1条回答 默认 最新

  • dotn30471 2014-12-09 16:22
    关注
    function fillTemplate(&$template, $client) {
        foreach ($template as $key => &$value) {
            if (is_array($value)) {
                fillTemplate($value, $client);
            } elseif (isset($client[$key])) {
                $value = $client[$key];
            }
        }
    }
    
    $result = $Template; // Make copy of template
    fillTemplate($result, $client);
    

    This walks the template recursively. When it reaches a leaf node, it replaces the value with the corresponding value from $client. It uses reference variables as the argument and in foreach so that the changes are made directly to the template. To preserve the original template, I make a copy of it before calling fillTemplate.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?