如果数组包含“0”,则将PHP CSV发送到数组;“我”;否则,如果
我正在从事一个php messenger项目,该项目将最多10行csv文件作为数组读入php。 我想将“我”消息与“发件人”列中的“您”消息隔离开来。我想把每个字符串放在一个不同的变量中(减去“From”列),其中最多有5个变量用于我,5个变量用于“you”消息 我想我需要在数组中执行foreach循环,如果数组的From列中包含“me”,则将string赋值给variable1,否则如果将string赋值给variable2等等 我正在努力想出数组上的foreach if的方法。 下面代码当前输出的示例数据如果数组包含“0”,则将PHP CSV发送到数组;“我”;否则,如果,php,arrays,foreach,Php,Arrays,Foreach,我正在从事一个php messenger项目,该项目将最多10行csv文件作为数组读入php。 我想将“我”消息与“发件人”列中的“您”消息隔离开来。我想把每个字符串放在一个不同的变量中(减去“From”列),其中最多有5个变量用于我,5个变量用于“you”消息 我想我需要在数组中执行foreach循环,如果数组的From列中包含“me”,则将string赋值给variable1,否则如果将string赋值给variable2等等 我正在努力想出数组上的foreach if的方法。 下面代码当前
From Time Message
me 03/02/2020 13:34 test message 001
you 03/02/2020 13:34 test message 002
me 03/02/2020 13:34 test message 003
you 03/02/2020 13:34 test message 004
me 03/02/2020 13:34 test message 005
you 03/02/2020 13:34 test message 006
me 03/02/2020 13:34 test message 007
you 03/02/2020 13:34 test message 008
me 03/02/2020 13:34 test message 009
you 03/02/2020 13:34 test message 010
欢迎来到StackOverflow
- 如果
数组的第一个索引是来自me或you的消息,则在$data
附近粘贴代码时似乎出现错误,只需在
循环内进行检查,然后将数组的其余部分分配给相应的结果数组(我或您)。试试下面的代码: 然后,对于输出:foreach
桌子{ 边界塌陷:塌陷; 宽度:100%; } th,td{ 文本对齐:左对齐; 填充:8px; } tr:n个孩子(偶数){ 背景色:#F2F2 } th{ 背景色:#4CAF50; 颜色:白色; } .我{ 颜色:红色; } .你{ 颜色:蓝色; } 从…起 时间 消息
更正了错误,谢谢。输出结果看起来就像手机上的短信。所以我想去掉“我”和“你”,通过css用不同的颜色表示它们。基本上我是在模仿短信。我假设您的if语句在我已有的while循环中使用?因此这是一个良好的开端,但是每个记录的输出必须进入一个单独的变量。“me”行将进入编号为me1、me2、me3等的变量,“you”行将进入编号为you1、you 2、you3等的变量。(每个最多5条,因此我们可以在输出上看到最后10条消息)这些变量将通过css以两种不同的颜色输出…并像手机上的短信一样堆叠。为什么需要这些变量?所需的输出可以通过2个数组构建
和$me
,老实说,我想用适当的css打印变量……但打印数组可能会起作用,但它们需要按收到的顺序打印,不是所有的me,然后是所有的you……可能最终是3行me,然后是2行you然后4条我的线,然后1条你的线……这将模拟短信。订单将不断变化,就像手机上的短信一样。我将编辑我的回复。我想这就是你想要的。我投了赞成票,但它给了我这样一个信息:谢谢你的反馈!声誉低于15的人所投的票将被记录,但不会改变公开显示的帖子分数。$you
<?php $filename = 'messenger.csv'; // Open the file for reading $datadump = []; if (($h = fopen("{$filename}", "r")) !== FALSE) { // Each line in the file is converted into an individual array that we call $$ // The items of the array are comma separated while (($data = fgetcsv($h, 1000, ",")) !== FALSE) { // Each individual array is being pushed into the nested array $datadump[] = $data; } // Close the file fclose($h); } // Display the code in a readable format $build = '<table><thead><th>From</th><th>Time</th><th>Message</th></thead><tbody>; foreach($datadump as $row) { $build .= '<tr>'; foreach($row as $item) { $build .= "<td>{$item}</td>"; } $build .= '</tr>'; } $build .= '</tbody></table>'; echo $build; ?>```
$row = 1; // First line index $me = []; // Me array; $you = []; // You array // Open the file for reading if (($file = fopen("example.csv", "r")) !== FALSE) { // Convert each line into the local $data variable while (($data = fgetcsv($file, 1000, ",")) !== FALSE) { // To skip the file headers if ($row == 1) { $row++; continue; } // Change how you want to output the result $message = "Message: $data[2] at $data[1]"; if($data[0] == "me") { $me[] = $message; } else if($data[0] == "you"){ $you[] = $message; } } // Close the file fclose($file); } // Array of me echo "<pre>"; print_r($me); echo "<pre/>"; // Array of you echo "<pre>"; print_r($you); echo "<pre/>";
$row = 1; // first line index $messages = []; // messages array // Open the file for reading if (($file = fopen("example.csv", "r")) !== FALSE) { // Convert each line into the local $data variable while (($data = fgetcsv($file, 1000, ",")) !== FALSE) { // skip the file headers if ($row == 1) { $row++; continue; } $messages[] = [ "sender" => $data[0], "datetime" => $data[1], "message" => $data[2] ]; } // Close the file fclose($file); } // Comparison function function date_compare($element1, $element2) { $datetime1 = strtotime($element1['datetime']); $datetime2 = strtotime($element2['datetime']); return $datetime1 - $datetime2; } // Sort the array usort($messages, 'date_compare');
<!DOCTYPE html> <html> <head> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #f2f2f2 } th { background-color: #4CAF50; color: white; } .me { color: red; } .you { color: blue; } </style> </head> <body> <table> <tr> <th>From</th> <th>Time</th> <th>Message</th> </tr> <?php foreach ($messages as $message) { $style = "class='me'"; if($message["sender"] == "you") { $style = "class='you'"; } ?> <tr <?php echo $style;?> > <td><?php echo $message["sender"]; ?></td> <td><?php echo $message["datetime"]; ?></td> <td><?php echo $message["message"]; ?></td> </tr> <?php } ?> </table> </body> </html>