CodeIgniter模板解析器将多维数组转换为单

时间:2018-03-22 18:15:03

标签: php arrays codeigniter templates multidimensional-array

我有$this->data

的多维数组
Array
(
    [icon] => http://localhost/uploads/icon.png
    [logo] => http://localhost/uploads/logo.svg
    [seo] => Array
        (
            [canonical] => http://localhost/profile/register/
            [title] => Register
        )
)

如何将上述内容更改为

Array
(
    [icon] => http://localhost/uploads/icon.png
    [logo] => http://localhost/uploads/logo.svg
    [canonical] => http://localhost/profile/register/
    [title] => Register
)

如果我在[icon]下面有另一个[seo](重复),第一个[icon]会被替换或忽略,或者当我像上面那样更改它时会发生什么?我需要将其转换为单个数组的原因是因为当我发送数据进行解析时,我无法使{canonical}{title}生效。

来自$this->data['seo']

Profile_Controller.php将从$this->data替换MY_Controller.php所以现在,我正在做一个解决方法,例如将这些seo数据放入另一个数组并使用{ {1}}但我认为还有另一种更好的解决方法......

1 个答案:

答案 0 :(得分:1)

使用foreach非常简单。只需将seo的数组键分配给主数组,然后从主数组中取消设置seo数组:

   <?php
    $arr = [
        'icon' => 'http://localhost/uploads/icon.png',
        'logo' => 'http://localhost/uploads/logo.svg',
        'seo' => [
                'canonical' => 'http://localhost/profile/register/',
                'title' => 'Register'
            ]
    ];

        echo '<pre>';

        print_r($arr);

        foreach ($arr['seo'] as $k => $v) {
            $arr[$k] = $v;
        }
        unset($arr['seo']);

        print_r($arr);

如果icon数组中存在重复的seo,则会覆盖主数组中的重复import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) # set pins as output GPIO.setup(4,GPIO.OUT) GPIO.setup(18,GPIO.OUT) GPIO.setup(17,GPIO.OUT) p = GPIO.PWM(17,80) p.start(40) p.ChangeDutyCycle(50) GPIO.output(18,GPIO.LOW) GPIO.output(4,GPIO.HIGH) print("4 is HIGH") time.sleep(5) p.ChangeDutyCycle(70)#speed change #change direction of motor spinning GPIO.output(4,GPIO.LOW) GPIO.output(18,GPIO.HIGH) print("18 is HIGH") p.stop() GPIO.cleanup()