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

これでうまくいきます。最後の2つのディレクトリが同じ順序を維持している限り、ディレクトリ構造を好きなだけ変更できます/dir/date

textURLの後に複数のコロンで区切ることにより、配列のセクションに必要な数の文字列を追加できます。例:/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
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

最初の配列に対して 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

入力とスクリプトをありがとう。実際、これらのスクリプトから、データを多次元配列に変換する方法についてかなりのことを学びました。残念ながら、どれも私が望んでいたとおりには機能しませんでした。この問題を調査して学んだことの 1 つは、「データを表示する別の方法はありますか?」ということです。この場合、私はそれを見つけました。すべてのファイルを検索し、ファイル名と関連テキストを出力するシェル スクリプト。

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 に答える