【问题标题】:php Foreach for an array inside another arrayphp Foreach 用于另一个数组中的数组
【发布时间】:2014-09-18 04:43:19
【问题描述】:

我不知道该使用什么标题,因为这是一种非常不寻常的情况。请让我解释一下。我有一个包含多个页面的网站,每个页面都有不同的元标记,例如标题、描述和关键字。所以我做了这个代码:

define('title', 0);
define('keywords', 1);
define('desc', 2);

$tags = array(
    'index'    => array('My site', 'php, tricks', 'My site about php'),    
    'about'    => array('About', 'etc, about', 'About this site'),
    'other'    => array('Other', 'some, shit', 'Some other shit')
);

我的网站页面如下所示:mysite.com/indexmysite.com/about 等,因此,我有一个目录,其中包含所有名为 index.phpabout.php 等的页面。因此,如果我想添加一个名为contact 的新页面,我必须创建一个名为contact.php 的文件并在我的数组中添加一个新行,如下所示:

$tags = array(
    'index'    => array('My site', 'php, tricks', 'My site about php'),    
    'about'    => array('About', 'etc, about', 'About this site'),
    'other'    => array('Other', 'some, shit', 'Some other shit'),
    'contact'  => array('Contact', 'contact, whatever', 'Contact form')
);

到目前为止,这工作正常。但是,现在我有一组以column- 开头的页面,我希望它们自动添加标签,而无需在代码中添加更多行。像这样的:

$tags = array(
    'index'    => array('My site', 'php, tricks', 'My site about php'),    
    'about'    => array('About', 'etc, about', 'About this site'),
    'other'    => array('Other', 'some, shit', 'Some other shit'),
    $column_pages  => array($col_title, $col_keywords, $col_desc)
);

我想我应该先扫描我的目录中的文件以获取名称,然后过滤那些以“column-”开头的文件:

$column_page = scandir("content");
foreach ($column_page as $value) {
  if(preg_match("/column/i", $value)) {
     $column_pages = $value;
  }
}

但是在这之后我有点卡住了,我不知道如何走得更远。我现在该怎么办?

我需要的是,说白了就是扫描特定文件夹/目录中的所有文件名,检查其名称是否以column-开头,然后生成属性在PHP的帮助下自动进行

例如,假设到目前为止我有3个以“column-”开头的文件,它们是:
column-newscolumn-varietycolumn-radio;我的预期结果与下面的代码完全一样:

$tags = array(
    'index'    => array('My site', 'php, tricks', 'My site about php'),    
    'about'    => array('About', 'etc, about', 'About this site'),
    'other'    => array('Other', 'some, shit', 'Some other shit'),
    'column-news'  => array('News', 'some, news', 'Column with news'),
    'column-variety'  => array('News', 'some, variety', 'Column with variety'),
    'column-radio'  => array('News', 'some, radio', 'Column with radio')
);

谢谢。

【问题讨论】:

  • 那么您到底在寻找什么...?你卡在哪里了,不清楚!
  • 我被困在这一点上,因为我不能再进一步了。变量 $column_pages 应该为名称以 column- 开头的所有文件生成名称-
  • 我编辑了我的问题,现在它更清晰了。

标签: php arrays foreach


【解决方案1】:

这是一种方法

  • 扫描所有目录

  • regex 匹配文件名中的模式

  • 如果找到匹配则存储在数组中

代码

$pattern = "/^column-*/i" //i for making it case insensitive, ^ denotes start of name
$path = "some/dir/path/";
$files = scandir($path);
foreach ($files as $file_name) {
    if(preg_match($pattern, $file_name){
        $column_pieces = explode("-", $file_name);
        $tags = array($file_name  => 
                        array($column_pieces[0], $column_pieces[1], $column_pieces[2])
                      );
    }
}

在这行代码中:

$tags = array($file_name  => 
                            array($column_pieces[0], $column_pieces[1], $column_pieces[2])
                          );

$file_name 将是您的密钥,$column_pieces[XX] 是可选的,仅用于向您展示如何完成,您可以将其他详细信息保存在其中,您可以从数据库中获取这些详细信息

【讨论】:

  • 恐怕我不明白。我如何存储在数组中?
  • 我明白了,但是那些不以 column- 开头的文件呢?
  • 您在目录中遵循的命名约定是什么??如果可行,只需使用column 而不是column- :)
  • 到目前为止,我是随机命名它们,没有特定的模式,但现在我添加了这些以 column- 开头的新文件。然而,由于我已经区分了旧文件的元标记,我想知道我应该将它们包含在你的代码中的什么位置。
  • 如果可以修改旧的元标记,我建议您这样做。这将在代码库中提供更多的一致性,并且将来也将易于管理!:)
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 2013-06-24
  • 2017-07-26
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多