2012-05-18 26 views
0

希望有人能指出我在這裏向正確的方向...創建從平面陣列新的多維數組 - 目錄路徑 - PHP

我有目錄路徑和部分文件輸出形成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迭代方法,但我只是不出來王牌。尋找任何指導。

感謝所有

+0

你可以發佈你已經嘗試過嗎? –

+1

目前還不清楚爲什麼每個條目有兩個「隨機」字符串... – Amarnasan

回答

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 

從這一點我可以很容易地得到這個信息在一個數組中。再次感謝所有

0

使第一陣列的preg_match()爲您希望從每一個字符串中提取信息的元素在一個foreach。

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

    if(preg_match("~(?<=section1/)[.-\w]*~i", $strElement, $astrMatches) >= 1) 
    $newArray['dir'] = $astrMatches[0]; 
    ...etc... 
} 
0

Explode路徑的每個字符串用「/".You將得到that.Then推後的陣列在陣列中所需的元件。

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 
     ) 

) 
+0

好,但爲什麼'for'循環而不是'foreach'? – TRiG

+0

沒理由,我現在也加了一個'foreach'的例子:) – vimist

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; 
} 
0

確定這會做jo b,只要最後兩個目錄保持相同的順序/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); 
}