帮助使用php explode()并组织输出
有人能告诉我如何正确地绘制地图吗?我试图理解如何使用php explode()并以一种可以检索和打印的方式组织这些值。对于每条记录,我想在一个特定的bucket中放一个name=value。我有(7)个最大桶每记录。有时我的记录无法填满每个桶 (例如,记录(2)缺少属性(5,6,7),记录(3)缺少属性(4)) 到目前为止,我写这篇文章是为了查看我的输出:帮助使用php explode()并组织输出,php,multidimensional-array,explode,Php,Multidimensional Array,Explode,有人能告诉我如何正确地绘制地图吗?我试图理解如何使用php explode()并以一种可以检索和打印的方式组织这些值。对于每条记录,我想在一个特定的bucket中放一个name=value。我有(7)个最大桶每记录。有时我的记录无法填满每个桶 (例如,记录(2)缺少属性(5,6,7),记录(3)缺少属性(4)) 到目前为止,我写这篇文章是为了查看我的输出: <?php $InputFile = file("test.txt"); foreach ($InputFile as $line){
<?php
$InputFile = file("test.txt");
foreach ($InputFile as $line){
$pieces = explode(";", $line);
//print $pieces[0];
//print $pieces[1];
//print $pieces[2];
print $pieces[3];
//print $pieces[4];
//print $pieces[5];
//print $pieces[6];
//print_r($line);
}
?>
如果您的输入数据是如此不规则,您可能需要用更多的代码来解析它,而不仅仅是一个简单的分解,或者准备它,使其与分解一样工作
在第二种情况下,可以使用查找字符串中的所有“;”字符,并检查它们后面的数字是否有序。如果跳过一个(或多个)号码,则应插入另一个“;”进行补偿。这种方式explode将创建一个空数组元素,并且所有生成的数组都应该对齐。虽然还不清楚您要完成什么,但希望此代码可以帮助您了解发生了什么 注意,在这里,我在主foreach中添加了另一个循环,以迭代行中的每个名称/值对:
<?php
$InputFile = file("test.txt");
$lineCnt = 0;
foreach ($InputFile as $line){
$lineCnt++;
echo "Processing line #" . $lineCnt. "\n";
$pieces = explode(";", $line);
foreach($pieces as $pair){
$pair = explode('=',$pair);
if (!empty($pair[1])){
print "\t".$pair[0] .' = ' . $pair[1] . "\n";
}
}
}
看看这是否有帮助
$splits = explode(';',$_POST['data']);
foreach($splits as $id => $item) {
preg_match('/(.*?)=(.*)/',$item, $matches);
$parts[$matches[1]][] = $matches[2];
}
print_r($parts);
这将为您提供一个包含键为1列、2列等的数组,该数组将包含一个相关值的数组。您可以按任何方式打印此数组
以下是输出:
[1-Column] => Array
(
[0] => host1.colo.sub
[1] => host1.colo.sub
[2] => host1.colo.sub
)
[2-Column] => Array
(
[0] => Fri Aug 13
[1] => Fri Aug 13
[2] => Fri Aug 13
)
[3-Column] => Array
(
[0] => db9.nfl.colo2.
[1] => pdb2.colo2.
[2] => gl3_lvm
)
[4-Column] => Array
(
[0] => 00:00:03
[1] => 04:00:02
)
[5-Column] => Array
(
[0] => 01:55:02
[1] => 04:48:06
)
[6-Column] => Array
(
[0] => 87.24 MB
[1] => 54.64 MB
)
[7-Column] => Array
(
[0] => Success
[1] => Success
)
您希望得到什么样的输出?您好,timdev,我希望每一列(1-7)都充当一个表的标题,然后值就是行。如果缺少第4列、第5列或第6列,则将打印空单元格或空白单元格。我希望这是有道理的。想象一下这个数据在一个简单的表中。谢谢,谢谢@aadravid。“数据”是我的$Inputfile吗?如何传入文本文件?谢谢。你的数据可以是任何东西。。。只需将$\u POST['data']替换为已将数据读入的变量即可。您可以按照timdev的建议使用文件(“test.txt”)来读取您的文件。贪婪的量词(.*)将使第一个匹配项也与字符串的其余部分匹配。当我们这样做的时候,最好也去掉懒惰的量词。最好使用负字符类/([^=]*)=([^;]*)/ok。。。我刚刚开始学习正则表达式,所以这将有助于。。。谢谢你的蜡笔提示。这很酷。谢谢preg_match似乎更合适。这正是我的设想。如果我想删除每列的数字前缀,该怎么办?从“1列”->“列”,“2列”->“列”。我们能坚持预赛吗?谢谢,是的。只需将n-移动到捕获组之外(将其放入自己的非捕获可选组中)preg_match(“~(?:1-)(列[^;]*?)?(?:2-)(列[^;]*);?(?:3-)(列[^;]*)?(?:4-)(列[^;]*);(?:5-)(列[^;]*);(?:6-)(列[^;]*);(?:7-)(列[^;]*),,,,($);;$;
$splits = explode(';',$_POST['data']);
foreach($splits as $id => $item) {
preg_match('/(.*?)=(.*)/',$item, $matches);
$parts[$matches[1]][] = $matches[2];
}
print_r($parts);
[1-Column] => Array
(
[0] => host1.colo.sub
[1] => host1.colo.sub
[2] => host1.colo.sub
)
[2-Column] => Array
(
[0] => Fri Aug 13
[1] => Fri Aug 13
[2] => Fri Aug 13
)
[3-Column] => Array
(
[0] => db9.nfl.colo2.
[1] => pdb2.colo2.
[2] => gl3_lvm
)
[4-Column] => Array
(
[0] => 00:00:03
[1] => 04:00:02
)
[5-Column] => Array
(
[0] => 01:55:02
[1] => 04:48:06
)
[6-Column] => Array
(
[0] => 87.24 MB
[1] => 54.64 MB
)
[7-Column] => Array
(
[0] => Success
[1] => Success
)
$InputFile = file("test.txt");
foreach ($InputFile as $line){
preg_match('~(1-Column[^;]*;?)?(2-Column[^;]*)?;?(3-Column[^;]*)?;?(4-Column[^;]*)?;?(5-Column[^;]*)?;?(6-Column[^;]*)?;?(7-Column[^;]*)?;?~',$line,$pieces);
$pieces = array_pad($pieces,8,'');
echo "<pre>";print_r($pieces);echo "</pre>";
}
Array
(
[0] => 1-Column=host1.colo.sub;2-Column=Fri Aug 13;3-Column=db9.nfl.colo2.;4-Column=00:00:03;5-Column=01:55:02;6-Column=87.24 MB;7-Column=Success;
[1] => 1-Column=host1.colo.sub;
[2] => 2-Column=Fri Aug 13
[3] => 3-Column=db9.nfl.colo2.
[4] => 4-Column=00:00:03
[5] => 5-Column=01:55:02
[6] => 6-Column=87.24 MB
[7] => 7-Column=Success
)
Array
(
[0] => 1-Column=host1.colo.sub;2-Column=Fri Aug 13;3-Column=pdb2.colo2.;4-Column=04:00:02;
[1] => 1-Column=host1.colo.sub;
[2] => 2-Column=Fri Aug 13
[3] => 3-Column=pdb2.colo2.
[4] => 4-Column=04:00:02
[5] =>
[6] =>
[7] =>
)
Array
(
[0] => 1-Column=host1.colo.sub;2-Column=Fri Aug 13;3-Column=gl3_lvm;5-Column=04:48:06;6-Column=54.64 MB;7-Column=Success;
[1] => 1-Column=host1.colo.sub;
[2] => 2-Column=Fri Aug 13
[3] => 3-Column=gl3_lvm
[4] =>
[5] => 5-Column=04:48:06
[6] => 6-Column=54.64 MB
[7] => 7-Column=Success
)