在PHP中循环每个数组元素

时间:2017-02-03 20:18:01

标签: php arrays

我正在寻找一种方法来读取文本文档的每一行作为数组元素。

CREATE TEMPORARY TABLE CustomerIDs (Customer_ID INT NOT NULL)

INSERT INTO CustomerIDs 
select customer_ID from web_order, items_table as a, items_table as b
 where (web_order.t1 = a.item_name and a.quantity) 
 and (web_order.t2 = b.item_name and b.quantity)
 and ((a.quantity < b.quantity))

DELETE FROM web_order WHERE Customer_ID IN (SELECT Customer_ID FROM CustomerIDs)

DROP TABLE CustomerIDs

任何提示都表示赞赏。

干杯!

3 个答案:

答案 0 :(得分:2)

$numbers = explode(PHP_EOL, $line); 

这是你想要的吗?

更新了PHP_EOL

答案 1 :(得分:1)

你可以节省很多工作。阅读数组并修剪\n和/或\rforeach()

$numbers = array_map('trim', file('nums.txt'));

foreach($numbers as $number) {
    // echo $number
}

答案 2 :(得分:0)

// Get the file content by path.
$file = file_get_contents($file);
// Break the file into array of lines.
$lines = preg_split('/\r*\n+|\r+/', $file);
// Remove last element as the last \r\n adds an extra element into the array.
array_pop($lines);

// Iterate over each line.
foreach($lines as $line_number => $line_content){

  // do something...
}