0

希望有人可以在这里为我指明正确的方向...

我有一个 unix grep 的目录路径和部分文件输出。我有一个来自这些输出的平面数组。现在我想做一点 PHP 魔术,把这个平面数组变成一个层次更高的多维数组,以便更精细的用户输出

当前数组;

array(7) {
  [0]=>
  string(160) "/home/user/data/section1/dir1/20120107/filename.txt:random text after the colon"
  [1]=>
  string(160) "/home/user/data/section1/dir1/20120108/filename.txt: More random text after the colon"
  [2]=>
  string(160) "/home/user/data/section1/dir2/20120107/filename.txt: More random text after the colon"
  [3]=>
  string(160) "/home/user/data/section1/dir2/20120108/filename.txt: More random text after the colon"
  [4]=>
  string(160) "/home/user/data/section1/dir3/20120107/filename.txt: More random text after the colon"
  [5]=>
  string(160) "/home/user/data/section1/dir3/20120106/filename.txt: More random text after the colon"
  [6]=>
  string(160) "/home/user/data/section1/dir3/20120108/filename.txt: More random text after the colon"
}

我真正想要的

array(1) {
    array(3) {
        ["dir"]=>
        string(4) "dir1"
        ["date"]=>
        string(8) "20120107"
        ["text"]=>
        array (2) {
          [0]=>
          string(160) "random text after the colon"
          [1]=>
          string(160) "More random text after the colon"
          }
    }
    array(3) {
        ["dir"]=>
        string(4) "dir1"
        ["date"]=>
        string(8) "20120108"
        ["text"]=>
        array (2) {
          [0]=>
          string(160) "More random text after the colon"
          [1]=>
          string(160) "More random text after the colon"
          }
    }
    array(3) {
        ["dir"]=>
        string(4) "dir2"
        ["date"]=>
        string(8) "20120107"
        ["text"]=>
        array (2) {
          [0]=>
          string(160) "More random text after the colon"
          [1]=>
          string(160) "More random text after the colon"
          }
    }
}

我已经尝试了很多 foreach 的 SPL 迭代器方法,但我只是没有胜出。寻找任何指导。

谢谢大家

4

6 回答 6

2

此代码(使用for循环):

<?php
$data[] = "/home/user/data/section1/dir1/20120107/filename.txt:random text after the colon";
$data[] = "/home/user/data/section1/dir1/20120108/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir2/20120107/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir2/20120108/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir3/20120107/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir3/20120106/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir3/20120108/filename.txt: More random text after the colon";

for($i = 0; $i < count($data); $i++) {
    $data[$i] = str_replace('/home/user/data/section1/','',$data[$i]);
    $tmp = explode('/', $data[$i]);

    $newData[$i] = array(
        'dir' => $tmp[0],
        'date' => $tmp[1]
    );

    $tmp = explode(':', $tmp[2]);

    $newData[$i]['fileName'] = $tmp[0];
    $newData[$i]['text'] = $tmp[1];
}

print_r($newData);
?>

或者这段代码(使用foreach循环):

<?php
$data[] = "/home/user/data/section1/dir1/20120107/filename.txt:random text after the colon";
$data[] = "/home/user/data/section1/dir1/20120108/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir2/20120107/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir2/20120108/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir3/20120107/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir3/20120106/filename.txt: More random text after the colon";
$data[] = "/home/user/data/section1/dir3/20120108/filename.txt: More random text after the colon";

foreach($data as $d) {
    $tmp = explode('/', str_replace('/home/user/data/section1/','',$d));
    $tmp2 = explode(':', $tmp[2]);

    $newData[] = array(
        'dir' => $tmp[0],
        'date' => $tmp[1],
        'filename' => $tmp2[0],
        'text' => $tmp2[1]
    );
}

print_r($newData);
?>

输出:

Array
(
    [0] => Array
        (
            [dir] => dir1
            [date] => 20120107
            [fileName] => filename.txt
            [text] => random text after the colon
        )

    [1] => Array
        (
            [dir] => dir1
            [date] => 20120108
            [fileName] => filename.txt
            [text] =>  More random text after the colon
        )

============ more data here ============

    [6] => Array
        (
            [dir] => dir3
            [date] => 20120108
            [fileName] => filename.txt
            [text] =>  More random text after the colon
        )

)
于 2012-05-18T09:34:35.750 回答
0

在第一个数组上创建一个 foreach 并 preg_match() 为要从每个字符串中提取的信息的元素。

foreach( $firstArray => $strElement )
{
   $newArray[] = array();

   if( preg_match( "~(?<=section1/)[.-\w]*~i", $strElement, $astrMatches) >= 1 )
     $newArray['dir'] = $astrMatches[0];
   ...etc...
}
于 2012-05-18T09:26:38.943 回答
0

用“/”分解每个路径字符串。之后你会得到一个数组。然后在数组中推送所需的元素。

于 2012-05-18T09:26:49.900 回答
0
function magic($array_of_strings)
{
    define('REGEX','_^/home/user/data/section1/(dir\d+)/(\d+)/filename.txt:(.*)$_');
    $ret_array = array();

    foreach($array_of_strings as $string) {
        if (preg_match(REGEX, $string, $matches)) {
            $ret_array []= array(
              'dir'=>$matches[1],
              'date'=>$matches[2],
              'text'=>$matches[3],
            );
        }
    }
    return $ret_array;
}
于 2012-05-18T09:49:39.883 回答
0

好的,这将完成这项工作,只要最后两个目录保持相同的顺序,您就可以随意更改目录结构/dir/date

您可以将任意数量的字符串添加到text数组的部分,方法是在 URL 后用多个冒号分隔它们。例如/blah/dir/date/filename.txt : string 1 : string 2

必须调用您的原始数组$array

享受:

foreach ($array as $string) {
   $temp = array();
   $temp["strings"] = explode(':', $string); //Convert the string into an array using `:` as a seperator
   $temp["path"] = explode('/', $temp["strings"][0]); //Convert the url into an array using `/` as a seperator (each directory is it's own entry)
   $path_count = count($temp["path"]); //Count number of directories in the url
   $output = array(
      "dir" => $temp["path"][$path_count - 3],
      "date" => $temp["path"][$path_count - 2],
      "text" => array()
   );
   foreach ($temp["strings"] as $index => $value) { //Loop through and add any additional text to array
      if ($index) {
         array_push($output["text"], trim($value));
      }      
   }
   print_r($output);
}
于 2012-05-18T10:14:03.110 回答
0

感谢大家的输入和脚本。实际上,我已经从这些脚本中学到了很多关于将数据按摩到多维数组中的知识。不幸的是,他们都没有完全按照我的意愿工作。我在研究这个问题时学到的一件事是,“还有另一种呈现数据的方法吗?” 在这种情况下,我找到了它。一个 shell 脚本,用于搜索所有文件,输出文件名,然后是相关文本。

find /home/user/data/section1 -name 'filename.txt' | xargs grep -il texttxet | 
   while read file 
       do
          echo "$file"
          grep -i  -A 4 texttxet "$file" 
       done

File:/home/user/data/section1/dir1/20120107/filename.txt
line1
line2
line3

File:/home/user/data/section1/dir1/20120108/filename.txt
line1
line2

File:/home/user/data/section1/dir2/20120108/filename.txt
line1
line2

从这一点开始,我可以轻松地在数组中获取此信息。再次感谢大家

于 2012-05-19T13:34:48.737 回答