将php数组过滤为仅包含与$ data中的$ name具有匹配值的元素

时间:2018-06-27 23:59:41

标签: php arrays filter glob

我有一个php脚本,可将所有文件夹保存在posts文件夹中,并将它们放入列表中。

我为每个用于存储发布日期和类别/标签信息等的文件夹的json文件分配了一个$ postinfo_str变量。

我还有一个$ pagetitle变量分配给每个文件夹的title.php包含文件。假设我在“ June 2018”存档页面上,该文件中的文本将为“ June 2018”。如果我说的是“教程”类别页面,那将是title.php中的文本。

在json文件中,我有:

{
     "Arraysortdate": "YYYYMMDD",
     "Month": "Month YYYY",
     "Category": ["cat1", "cat2", "etc"]
 }

我正在使用以Arraysortdate为键的krsort来排序最新到最旧的数组。

如何使用$ pagetitle作为输入过滤数组,查找$ postinfo_str中是否存在匹配项,如果不存在,则从数组中删除该文件夹?

我似乎能找到的所有关于数组排序的地方是$ pageinfo_str中的信息基本上是数组,因此,$ title是输入,输出是$ postinfo_str中的匹配文本,而我想要输出将是在$ postinfo_str中仅具有与输入内容($ pagetitle)匹配的文本的文件夹。

这是我的代码。请记住,这是平面文件,我不希望数据库实现此目的。如果需要解释,请参阅评论。

<?php 

	$BASE_PATH = '/path/to/public_html';
	
	// initial array containing the dirs
	    $dirs = glob($BASE_PATH.'/testblog/*/posts/*', GLOB_ONLYDIR);

	// new array with date as key
	    $dirinfo_arr = [];
	    foreach ($dirs as $cdir) {

		// get current page title from file
		    $pagetitle = file_get_contents("includes/title.php");
	
		// get date & post info from file
		    $dirinfo_str = file_get_contents("$cdir/includes/post-info.json");
		    $dirinfo = json_decode($dirinfo_str, TRUE);

		// add current directory to the info array
		    $dirinfo['dir'] = $cdir;
		// add current dir to new array where date is the key
		    $dirinfo_arr[$dirinfo['Arraysortdate']] = $dirinfo;
	}
	// now we sort the new array
	    krsort($dirinfo_arr);

	    foreach($dirinfo_arr as $key=>$dir) {
	    	$dirpath = $dir['dir'];
		$dirpath = str_replace('/path/to/public_html/', '', $dirpath);

?>


       <!--HTML HERE SUCH AS--!>
       
           <a href="<?=$dirpath;?>"> TEXT </a><br>

<?php
};
?>


1 个答案:

答案 0 :(得分:0)

按照您的问题描述,我遇到了困难。您的代码示例有些混乱。它似乎为每个目录加载相同的 global includes/title.php。意思是,$pagetitle的值应该在每次迭代中都相同。如果打算这样做,则可能应将该行移到循环外部。如果文件包含实际的php代码,则可能应该使用

$pagetitle = include 'includes/title.php'; 

或类似的东西。如果没有,您应该将其命名为title.txt。如果不是一个全局文件,则可能还应该将路径添加到file_get_contents / include。 (但是,为什么不只在json结构中添加标题?)

我以为这是在尝试提供最小的代码示例(?)时偶然发生的...在任何情况下,我的答案都不会是完美的答案,但是希望一旦理解后就可以改编。 ; o)

如果只希望数组中的元素满足某些属性,则基本上有两种选择:

  1. 不要将这些元素放入(主要是您的代码)

    foreach ($dirs as $cdir) {
    
        // get current page title from file
        $pagetitle = file_get_contents("includes/title.php");
    
        // get date & post info from file
        $dirinfo_str = file_get_contents("$cdir/includes/post-info.json");
        $dirinfo = json_decode($dirinfo_str, TRUE);
    
        // add current directory to the info array
        $dirinfo['dir'] = $cdir;
        // add current dir to new array where date is the key
    
     // ------------ NEW --------------
        $filtercat = 'cat1';
        if(!in_array($filtercat, $dirinfo['Category'])) {
             continue;
        }
     // -------------------------------
    
        $dirinfo_arr[$dirinfo['Arraysortdate']] = $dirinfo;
    
  2. array_filter之后,通过提供匿名函数

     // ----- before cycling through $dirinfo_arr for output
     $filtercat = 'cat1';
     $filterfunc = function($dirinfo) use ($filtercat) {
         return in_array($filtercat, $dirinfo['Category']));
     }
    
     $dirinfo_arr = array_filter($dirinfo_arr, $filterfunc);
    

    您应该阅读有关匿名函数的知识,以及如何为它们提供本地变量,以减轻痛苦。也许您的用例更适合array_reduce,这很相似,除了您可以确定“过滤器”的输出。

$new = array_filter($array, $func),只是一种奇特的写作方式:

$new = [];
foreach($array as $key => $value) {
    if($func($value)) {
        $new[$key] = $value;
    }
}

更新1

在我的代码示例中,如果要匹配json-struct(基本级别)中的任何内容,则可以用in_array($filtercat, $dirinfo['Category'])替换in_array($pagetitle, $dirinfo)-如果要匹配json-struct(基本级别)中的任何内容,也可以替换为($pagetitle == $dirinfo['Month'])只是想匹配月份。

更新2

我了解,您可能只是从php甚至是编程开始,因此某些“大型数据库”的概念可能令人恐惧。但是,从某种角度来看,文件系统也是一个数据库。但是,相比起来,它通常很慢,并且也没有提供很多功能。

从长远来看,我强烈建议使用数据库。如果您不喜欢将数据放入“某些数据库服务器”的想法,请使用sqlite。但是,如果您以前从未处理过数据库,则涉及学习曲线。从长远来看,这将是值得花费的时间,因为它可以简化很多事情。

相关问题