PHP пока перебирает массивы - PullRequest
       0

PHP пока перебирает массивы

0 голосов
/ 20 сентября 2011

Привет всем, у меня проблемы с правильным вложением циклов while для чтения из 2 массивов.У меня есть 2 файла, из которых я читаю содержимое:

file1: item_list.txt
string1 \n
string2 \n
string3 \n
...

file2: item+info.txt
string3 \t info1 \t info2 \t info3
string1 \t info7 \t info1 \t info4
string5 \t info2 \t info3
string2 \t info2 \t info4 \t info1

(значения разделены только новыми строками и вкладками, я добавил один пробел между символами здесь только для повышения читабельности) .

Я читаю из файлов, используя функцию fgetcsv () , и каждая строка из файла сохраняется в виде массива в переменную $ data .Я создал цикл while с условием (! Feof ($ fp)) для чтения файла до последней строки.Но я не могу должным образом вложить второй цикл.

Что я хочу сделать с этим: прочитать первую строку, найденную в file1, перейти к file2 и попытаться найти эту строку.Если есть совпадение, получите информационные данные для этой строки (все данные, или только одно, не имеют значения).Если совпадения нет, верните сообщение «нет совпадения».В любом случае, как только второй цикл завершит свою работу, мне нужно прочитать вторую строку в файле1 и снова выполнить поиск в файле2.Повторяйте это, пока есть что прочитать из файла 1.

Вот две версии моего кода, они не работают, и я не могу понять, почему.

//opening the files
$fp = fopen("$DOCUMENT_ROOT/test/item_list.txt", "r"); #raw item list
$pf = fopen("$DOCUMENT_ROOT/test/item+info.txt", "r"); #item+info list

//read from first file
$line=0;
while (!feof($fp)){
    $line++;
    $data1 = fgetcsv($fp, "1000", "\n");
    $item1= $data1[0];
    echo "line: $line. item_list: ".$item1; //just to see on screen what's happening
    print_r($data1); //same here, just to see what's going on
    echo"<br />";

    //searching for string in file2
    $row=0;
    while (!feof($pf)){
        $row++;
        $data2 = fgetcsv($pf, "1000", "\t");
        $item2= $data2[0];
        echo "line: $row. item+info: ".$item2; //just checking things on screen
        print_r($data2); //here too
        echo "<br />";

        //conditioning
        //equal strings
        if ($string1== $string2)
            echo $data2[1]."<br />";
        break;
    }
}

fclose($fp);
fclose($pf);

это работало до тех пор, пока элементы в item_list.txt и item + info.txt рудерированы
точно так же (string1 \ nstring2 \ string3 ->
string1 \ tinfo1\ nstring2 \ tinfo2 \ nstring3 \ tinfo3 - но в моем случае этого никогда не произойдет, упорядочить подобные элементы невозможно)

Я пытался сделать это с помощью оператора foreach (), чтобы сделать это через массивы, норезультат чего-то, из чего я не могу разобраться.

while (!feof($fp)){
    $data1 = fgetcsv($fp);
    foreach ($data1 as $token1) {
        while (!feof($pf)) {
            $data2 = fgetcsv($pf);
            foreach ($data2 as $value) {
                explode ("\t", $value);
                if ($token1 == $value[0])
                    echo $value[1];
            } 
            break;
        } 
    }
}

Ответы [ 3 ]

1 голос
/ 20 сентября 2011

Это должно сделать это:

$file1 = file($DOCUMENT_ROOT . '/test/item_list.txt');
$file2 = file($DOCUMENT_ROOT . '/test/item+info.txt');

foreach ($file1 as $line)
{
    $line = rtrim($line); // just in case ...
    if ($line === '') continue;

    foreach($file2 as $infoline)
    {
        $infoline = explode("\t", rtrim($infoline);
        if ($line === $infoline[0])
        {
            array_shift($infoline);
            echo $line . '<br /><br />' . implode('<br />', $infoline);
            // $results[$line] = $infoline; // uncomment this if you need the search results stored for later use
            break;
        }
    }
}
0 голосов
/ 20 сентября 2011

Это на самом деле гораздо меньше кода, чем вы думаете.Сначала вы читаете инфо-файл и строите из него хеш-таблицу:

foreach(file("info_list") as $line) {
    $line = explode("\t", trim($line));
    $info[$line[0]] = $line;
}

, затем перебираете файл элементов и смотрите, есть ли в хэше совпадающие записи:

foreach(file("item_list") as $line) {
    $item = trim($line);
    if(isset($info[$item]))
        // we have some info for this item 
    else
        // we have no info for this item
}

это в основном все об этом

0 голосов
/ 20 сентября 2011

Вот пример:

$filename1 = 'item_list.txt';
$filename2 = 'item+info.txt';

# Init the Raw Array
$content2raw = array();

# Get the File Contents for File 2
$file2 = file_get_contents( $filename2 );
# Split it into an Array by Line Breaks
$content2raw = preg_split( "/\n/" , $file2 , -1 , PREG_SPLIT_NO_EMPTY );

# Unset the variable holding the file contents
unset( $file2 );

# Init the Fixed Array
$content2 = array();

# Loop through the Raw Array
foreach( $content2raw as $l ){
  // Each Line of Filename2
  # Split the Line on Tabs
  $t = preg_split( "/\s*\t\s*/" , $l , -1 );
  # Set the Fixed Array, using the first element from the line as the key
  $content2[ $t[0] ] = $t;
}

# Unset the Raw Array
unset( $content2raw );

# Get the File Contents from File 1
$file1 = file_get_contents( $filename1 );
# Split it into an Array by Line Breaks
$contents1 = preg_split( "/\n/" , $file1 , -1 , PREG_SPLIT_NO_EMPTY );

# Unset the variable holding the file contents
unset( $file1 );

# Loop through the Lines, using each line as the Key to look for
foreach( $content1 as $v ){
  # Check whether a matching element exists in the array from File 2
  if( !array_key_exists( $k , $content2 ) ){
    // No Match Found
    echo 'No Match';
  }else{
    // Match Found
    echo 'Match Found';
    var_dump( $content2[$v] );
  }
}

Поправка, согласно комментарию / отзыву от @ Bluewind

$filename1 = 'item_list.txt';
$filename2 = 'item+info.txt';

# Open and Split the file into an Array by Line
$content2raw = file( $filename2 );

# Init the Fixed Array
$content2 = array();

# Loop through the Raw Array
foreach( $content2raw as $l ){
  // Each Line of Filename2
  # Split the Line on Tabs
  $t = preg_split( "/\s*\t\s*/" , $l , -1 );
  # Set the Fixed Array, using the first element from the line as the key
  $content2[ $t[0] ] = $t;
}

# Unset the Raw Array
unset( $content2raw );

# Open and Split the file into an Array by Line
$contents1 = file( $filename1 );

# Loop through the Lines, using each line as the Key to look for
foreach( $content1 as $v ){
  # Check whether a matching element exists in the array from File 2
  if( !array_key_exists( $k , $content2 ) ){
    // No Match Found
    echo 'No Match';
  }else{
    // Match Found
    echo 'Match Found';
    var_dump( $content2[$v] );
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...