2016-09-18 33 views
0

我有一個很難做this..so我有以下文本文件拆分文本到一個多維數組PHP

The Boys in the Boat Daniel James Brown 067002581X 19.99 5 16.99 9 16.99 
Harry Potter and the Cursed Child J. K. Rowling, Jack Thorne, John Tiffany 1338099133 18.95 25 17.98  0 17.98 
Just Mercy Bryan Stevenson 0812994520 17.50 8 16.25 10 16.25 
Me Before You Jojo Moyes 0670026603 18.95 2 17.50 1 17.25 
A Thousand Splendid Suns Khaled Hosseini 1594489505 19.00 7 15.50 4 14.95 
The Wright Brothers David McCullough 1476728742 21.95 3 18.95 3 18.95 

我需要以某種方式使用函數來讀入一個2維關聯數組.. 我沒有問題,從頭開始創建INFACT我已經做了陣列,即...,陣列應該像

$books = array( 
     "The Boys in the Boat" => array (
      "author" => 'Daniel James Brown', 
      "isbn" => '067002581X', 
      "hardcover" => 19.99, 
      "quantity" => 5, 
      "softcover" => 5.99, 
      "e-book" => 6.99, 
     ), 
"Jungle" => array (
      "author" => 'Upton Sinclair', 
      "isbn" => '067002581', 
      "hardcover" => 19.99, 
      "quantity" => 5, 
      "softcover" => 5.99, 
      "e-book" => 6.99, 
     ), 



    ); 

我不知道如何創造條件,經過一個功能逐行掃描文件文本並製作二維數組...我知道我必須這樣做使用爆炸,但我不知道使用什麼樣的分隔符,因爲如果我使用了空格,它將無法工作,並且文件中沒有其他的分隔符。請幫助我花了整整一天的時間...

+0

它看起來像字段是製表符分隔,所以拆分對製表符(''\ t'')。 – smarx

+0

偉大的是有道理的....現在我怎麼讓鑰匙成爲我自己的價值觀,如作家,isbn等?謝謝 –

+1

爲什麼你不分享你到目前爲止的代碼和你被卡住的地方? – smarx

回答

0

這就是我想出來的。

請注意代碼中的註釋,並且請注意,我認爲您的示例文件在此處發佈時已更改(製表符爲空格)。

如果你有一定數量的空格作爲分隔符,這會變得更加困難,因爲我們在一個字段內可能有相同的一組空白空間(例如書名)。這可能會導致腳本無法按預期運行。你可以將"\t"更改爲" "(四個空格),如果這是你的,你需要測試文件/字符串來知道。

選項卡沒有問題,但是在任何文本字段中都不能有選項卡,否則這將失敗。

因爲這裏的空格改變了標籤,我在下面的代碼中將它們硬編碼爲\t

<?php 
# This is basically just exploding the elements 2 times 
# and getting the elements in the right order 

# This will hold the array with info 
$finalResult = array(); 

# Your input text to be parsed 
# I am supposing you have TABS as delimiters and the order never changes 
$inputText = <<<EOF 
The Boys in the Boat\tDaniel James Brown\t067002581X\t19.99\t5\t16.99\t9\t16.99 
Harry Potter and the Cursed Child\tJ. K. Rowling, Jack Thorne, John Tiffany\t1338099133\t18.95\t25\t17.98\t0\t17.98 
Just Mercy\tBryan Stevenson\t0812994520\t17.50\t8\t16.25\t10\t16.25 
Me Before You\tJojo Moyes\t0670026603\t18.95\t2\t17.50\t1\t17.25 
A Thousand Splendid Suns\tKhaled Hosseini\t1594489505\t19.00\t7\t15.50\t4\t14.95 
The Wright Brothers\tDavid McCullough\t1476728742\t21.95\t3\t18.95\t3\t18.95 
EOF; 

# First break each line into array of lines 
# If you are sure this file comes from *nix you can use \n as newline 
# If you are sure this comes from Windows you can use \r\n 
# Or you can use PHP_EOL but this use the current system as a basis for what to use 
$textLines = explode("\n", $inputText); 

# Now we go through each line getting all data 
foreach($textLines as $line) { 
    # Get each tab-separated field 
    $expLine = explode("\t", $line); 
    # Sanity check 
    if (count($expLine) < 8) { 
     # The line does not have enough items, deal with error 
     echo "Item " . (isset($expLine[0]) ? $expLine[0]." " : "") . "ignored because of errors\n"; 
     continue; 
    } 
    # new item 
    # I have changed this a bit as it seems you have more fields to get than 
    # what shows on your example (2 quantities, for hard and softcovers) 
    $finalResult[$expLine[0]] = array(
     "author"  => $expLine[1], 
     "isbn"  => $expLine[2], 
     "hardcover" => $expLine[3], 
     "hc-quantity" => $expLine[4], 
     "softcover" => $expLine[5], 
     "sc-quantity" => $expLine[6], 
     "e-book"  => $expLine[7], 
    ); 
} 

# Just show the data structure 
var_dump($finalResult); 

?> 

,然後將結果:

array(6) { 
    ["The Boys in the Boat"]=> 
    array(7) { 
    ["author"]=> 
    string(18) "Daniel James Brown" 
    ["isbn"]=> 
    string(10) "067002581X" 
    ["hardcover"]=> 
    string(5) "19.99" 
    ["hc-quantity"]=> 
    string(1) "5" 
    ["softcover"]=> 
    string(5) "16.99" 
    ["sc-quantity"]=> 
    string(1) "9" 
    ["e-book"]=> 
    string(5) "16.99" 
    } 
    ["Harry Potter and the Cursed Child"]=> 
    array(7) { 
    ["author"]=> 
    string(40) "J. K. Rowling, Jack Thorne, John Tiffany" 
    ["isbn"]=> 
    string(10) "1338099133" 
    ["hardcover"]=> 
    string(5) "18.95" 
    ["hc-quantity"]=> 
    string(2) "25" 
    ["softcover"]=> 
    string(5) "17.98" 
    ["sc-quantity"]=> 
    string(1) "0" 
    ["e-book"]=> 
    string(5) "17.98" 
    } 
    ["Just Mercy"]=> 
    array(7) { 
    ["author"]=> 
    string(15) "Bryan Stevenson" 
    ["isbn"]=> 
    string(10) "0812994520" 
    ["hardcover"]=> 
    string(5) "17.50" 
    ["hc-quantity"]=> 
    string(1) "8" 
    ["softcover"]=> 
    string(5) "16.25" 
    ["sc-quantity"]=> 
    string(2) "10" 
    ["e-book"]=> 
    string(5) "16.25" 
    } 
    ["Me Before You"]=> 
    array(7) { 
    ["author"]=> 
    string(10) "Jojo Moyes" 
    ["isbn"]=> 
    string(10) "0670026603" 
    ["hardcover"]=> 
    string(5) "18.95" 
    ["hc-quantity"]=> 
    string(1) "2" 
    ["softcover"]=> 
    string(5) "17.50" 
    ["sc-quantity"]=> 
    string(1) "1" 
    ["e-book"]=> 
    string(5) "17.25" 
    } 
    ["A Thousand Splendid Suns"]=> 
    array(7) { 
    ["author"]=> 
    string(15) "Khaled Hosseini" 
    ["isbn"]=> 
    string(10) "1594489505" 
    ["hardcover"]=> 
    string(5) "19.00" 
    ["hc-quantity"]=> 
    string(1) "7" 
    ["softcover"]=> 
    string(5) "15.50" 
    ["sc-quantity"]=> 
    string(1) "4" 
    ["e-book"]=> 
    string(5) "14.95" 
    } 
    ["The Wright Brothers"]=> 
    array(7) { 
    ["author"]=> 
    string(16) "David McCullough" 
    ["isbn"]=> 
    string(10) "1476728742" 
    ["hardcover"]=> 
    string(5) "21.95" 
    ["hc-quantity"]=> 
    string(1) "3" 
    ["softcover"]=> 
    string(5) "18.95" 
    ["sc-quantity"]=> 
    string(1) "3" 
    ["e-book"]=> 
    string(5) "18.95" 
    } 
} 
+1

爲我工作...謝謝你... –

0

由於之間的所有的這些都是2個或更多字符的空間:

//create books array 
$books = array(); 

//$text to array by each line 
$arr = explode("\n",$text); 

//explode and store each line 
foreach ($arr as $line){ 
$arr2 = explode(' ',$line); 
$books[] = array (
        'name'=>trim($arr2[0]), 
        'author'=>trim($arr2[1]), 
        'isbn'=>trim($arr2[2]), 
        'hardcover'=>trim($arr2[3]), 
        'quantity'=>trim($arr2[4]), 
        'softcover'=>trim($arr2[5]), 
        'e_book'=>trim($arr2[6]) 
       ); 
} 

//the array that you want 
print_r($books); 

PHP允許它,但我沒有用名數組裏面的關鍵,因爲它有空間在裏面。如果你堅持,你可以改變這一行

$books[] = array (

$books[$arr2[0]] = array (
0

考慮每一行是一個子陣,每本書的名字是一個索引,你可以使用的preg_match由2個或更多的空間分割,返回數組的第一項是書名(索引),另一個是子數組中的值。也就是說,你可以寫這樣的事情:

<?php 
$book_info_keys = array('author', 'isbn', 'hardcover', 'quantity', 'softcover', 'e-book'); 
$input_file = 'a.txt'; 

$result = array(); 
foreach (file($input_file) as $line) { // Read the file as an array 
    // On each line, split items by 2 or more spaces 
    $a = preg_split('/ {2,}/', $line); 

    // Now we divide the line into two part: the first item is book name, 
    // the rest is book info. 
    $book_name = $a[0]; 
    $book_info = array(); 

    $i = 1; // $a[0] is book name, so the first info is at index 1 
    foreach ($book_info_keys as $key) { 
    $book_info[$key] = $a[$i++]; 
    } 
    // Now insert the infos into the result array, with book name served as index 
    $result[$book_name] = $book_info; 
} 

var_dump($result); 
?> 

而且,記住上面的代碼不寫在現實生活中使用。 在任何地方添加錯誤檢查,永遠不要假定輸入文件始終有效(正如我在上面的代碼中所假設的那樣)。也許在某些行上有8個,9個或更多的項目,而另外的項目中只有6個,5個或更少的項目。在這些情況下,上面的代碼將會失效。

+0

爲了獲得更好的性能,在可能的情況下使用explode代替preg_split,並且也可以拋棄第二個foreach並直接添加它。 – ICE