PHP - 在同一个数组中使用相同的数组值

时间:2017-05-15 12:50:53

标签: php arrays

我有一个很长的数组,例如:

def global_settings
# if new_record? # Change this to
  if self.new_record?
  self.images.each { |image| image.update_attributes(
      to_what: self.to_what,
      added_to: self.added_to,
  )
  }
end

我希望从$userLogs = array( 'report-abuse-key' => array( 'name' => null, 'path' => null, 'code' => 'report-abuse-page', 'header' => 'Would you like to proceed?', 'link-in-navigation-menu'=>null, 'navigation-menu-in-page'=>'Show', 'meta'=>array( 'show'=>null, 'title'=>'//GET HEADER VALUE HERE//', 'description'=>null, 'keywords'=>null, 'refresh'=>null, 'canonical'=>null, 'noindex-tag'=>'Show',//'null' = Do SEO; 'Show' = No SEO; ), ), ); 获取值,并将其复制到$userLogs['header']中,即我希望它能够在不使用复制/粘贴的情况下自动复制和更新。

1 个答案:

答案 0 :(得分:1)

您可以使用php reference。您需要将header变量的引用分配给title变量。在此之后,title值将引用headerheader中的任何更新都会更新title

<?php
$userLogs = array(
    'report-abuse-key' => array(
        'name' => null,
        'path' => null,
        'code' => 'report-abuse-page',
        'header' => 'Would you like to proceed?',
        'link-in-navigation-menu'=>null,
        'navigation-menu-in-page'=>'Show',
        'meta'=>array(
            'show'=>null,
            'title'=>'//GET HEADER VALUE HERE//',
            'description'=>null,
            'keywords'=>null,
            'refresh'=>null,
            'canonical'=>null,
            'noindex-tag'=>'Show',//'null' = Do SEO; 'Show' = No SEO;  
            ),
        ),
);

$userLogs['report-abuse-key']['meta']['title'] = &$userLogs['report-abuse-key']['header'];

$userLogs['report-abuse-key']['meta']['title'] = 'Updated';
echo '<pre>';
print_r($userLogs);exit;

结果

Array
(
    [report-abuse-key] => Array
        (
            [name] => 
            [path] => 
            [code] => report-abuse-page
            [header] => Updated
            [link-in-navigation-menu] => 
            [navigation-menu-in-page] => Show
            [meta] => Array
                (
                    [show] => 
                    [title] => Updated
                    [description] => 
                    [keywords] => 
                    [refresh] => 
                    [canonical] => 
                    [noindex-tag] => Show
                )

        )

)