php从输入文件中选择特定内容放入数组元素

发布于 2024-10-20 06:03:58 字数 1022 浏览 0 评论 0原文

我有一个输入文件(从如下所示的文件中执行),其中包含多行,我需要从中选择特定文本并将每个选择放入数组元素中:

从输入文件中执行:

"BLOCK","PARTNO"

"ELEMENT","HEADER-"

"NAME","1AB000072186"

"REVISION","0000"

"PARTSHAPE","RECT_074_044_030"

"PACKAGE","120830E"

"PMABAR",""

"PARTCOMMENT","CAP-TANT*150uF*20%*10V7343*4.3mm"

"ELEMENT","PRTIDDT-"

"PMAPP",1

"PMADC",2

"ComponentQty",2

"BLOCK","PARTNO"

"ELEMENT","HEADER-"

"NAME","1AB030430005"

"REVISION","0000"

"PARTSHAPE","RECT_072_042_030"

"PACKAGE","120830E"

"PMABAR",""

"PARTCOMMENT","1.0000 Amp SUBMINIATURE FUSE"

"ELEMENT","PRTIDDT-"

"PMAPP",2

"PMADC",0

"ComponentQty",1

"BLOCK","PARTNO"

"ELEMENT","HEADER-"

"NAME","1AB030430001"

"REVISION","0000"

"PARTSHAPE","RECT_072_042_030"

"PACKAGE","120830E"

"PMABAR",""

"PARTCOMMENT","2.0000 Amp SUBMINIATURE FUSE"

"ELEMENT","PRTIDDT-"

"PMAPP",2

"PMADC",0

"ComponentQty",1

请注意,在每次出现带有短语“ComponentQty”的行之后“内容开始重复...

其中我需要数组元素的一维中“NAME”出现旁边的 PartNumber 以及每个元素的第二个维度中“PARTSHAPE”出现旁边的内容。我对如何做到这一点感到非常困惑...请帮助!

I have an input file (exert from file shown below) with multiple lines that I need to select specific text from and put each selection into an array element:

exert from input file:

"BLOCK","PARTNO"

"ELEMENT","HEADER-"

"NAME","1AB000072186"

"REVISION","0000"

"PARTSHAPE","RECT_074_044_030"

"PACKAGE","120830E"

"PMABAR",""

"PARTCOMMENT","CAP-TANT*150uF*20%*10V7343*4.3mm"

"ELEMENT","PRTIDDT-"

"PMAPP",1

"PMADC",2

"ComponentQty",2

"BLOCK","PARTNO"

"ELEMENT","HEADER-"

"NAME","1AB030430005"

"REVISION","0000"

"PARTSHAPE","RECT_072_042_030"

"PACKAGE","120830E"

"PMABAR",""

"PARTCOMMENT","1.0000 Amp SUBMINIATURE FUSE"

"ELEMENT","PRTIDDT-"

"PMAPP",2

"PMADC",0

"ComponentQty",1

"BLOCK","PARTNO"

"ELEMENT","HEADER-"

"NAME","1AB030430001"

"REVISION","0000"

"PARTSHAPE","RECT_072_042_030"

"PACKAGE","120830E"

"PMABAR",""

"PARTCOMMENT","2.0000 Amp SUBMINIATURE FUSE"

"ELEMENT","PRTIDDT-"

"PMAPP",2

"PMADC",0

"ComponentQty",1

Notice that after each occurrence of the line with the phrase "ComponentQty" the content begins repeating...

Where I need the PartNumber that is next to the occurrence of "NAME" in one dimension of the array element and the content next to the occurrence of "PARTSHAPE" in the second dimension for each element. I am very confused on how to do this though...please help!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里南柯 2024-10-27 06:03:58
$fh = fopen('yourfile.txt', 'rb');

$found_stuff = array();
$last_component = null;

while($line = fgets($fh)) { // read a line
   $parts = explode(',', $line);  // split into components
   switch($parts[0]) {  // based on which key we're on
       case '"NAME"':
            $last_component = $parts[1];   // save the key's value
            break;
       case '"PARTSHAPE"':
            $found_stuff[$last_component] = $parts[1];  // store the partshape name
            break;
   }
}
fclose($fh);

这应该完成基本工作。阅读一行,将其分解为逗号出现的部分。第一部分将是“键”,第二部分将是值。然后继续阅读,直到我们点击 NAME 或 PARTSHAPE 键,然后根据需要存储值。

请注意,我没有去掉值中的双引号。这留给读者作为练习。此代码还假设文件的格式是常规的,并且“NAME”将显示在任何 PARTSHAPE 行之前,并且 NAME/PARTSHAPE 行之间将存在完美的 1:1 交替。如果您连续获得两个 PARTSHAPES,您将失去第一个。如果 PARTSHAPE 在第一个 NAME 出现之前出现,那么您也会丢失该名称。

$fh = fopen('yourfile.txt', 'rb');

$found_stuff = array();
$last_component = null;

while($line = fgets($fh)) { // read a line
   $parts = explode(',', $line);  // split into components
   switch($parts[0]) {  // based on which key we're on
       case '"NAME"':
            $last_component = $parts[1];   // save the key's value
            break;
       case '"PARTSHAPE"':
            $found_stuff[$last_component] = $parts[1];  // store the partshape name
            break;
   }
}
fclose($fh);

This should do the basic work. Read a line, explode it into pieces where commas occur. The first part will be the "key", the second part will be the value. Then simply keep reading until we either hit a NAME or a PARTSHAPE key, then store the values as appropriate.

Note that I've not stripped the double-quotes off the values. That's left as an exercise to the reader. This code also assumes that the file's format is regular and that a "NAME" will show up before any PARTSHAPE lines, and there'll be a perfect 1:1 alternation between NAME/PARTSHAPE lines. If you get two PARTSHAPES in a row, you'll lose the first one. And if a PARTSHAPE shows up before the first NAME is encounted, you'll sorta lose that one too.

温柔女人霸气范 2024-10-27 06:03:58

以下步骤对我有用:

粘贴在我的OP中的部分(重复多次)被定义为$PartNoContents
$BlockData[] 是我需要将 $PartNoContents 中的选择粘贴到的数组。

$PartNoContents = str_replace('"', '', $PartNoContents);
$PartLines = explode("\n", $PartNoContents);
$PartData = array();
foreach ($PartLines as $PartLine){
    $PartData[] = explode(',', $PartLine);
}

for($p=0;$p<count($PartLines);$p++){
   if ( isset( $PartData[$p][1] ) && !empty( $PartData[$p][1] ) ){
       $p1 = str_replace(chr(13), '', $PartData[$p][1]);
       if ( isset($BlockData[$b][0]) && !empty($BlockData[$b][0]) && $BlockData[$b][7]==$p1 ){
           $BlockData[$b][13] = str_replace(chr(13), '', $PartData[$p+$PartDataIncNum][1]);
           $p = count($PartLines) ;
       }
   }
}

The following steps worked for me:

The section pasted in my OP (repeating many times more) is defined as $PartNoContents
and $BlockData[] is the array that I need to paste selections from $PartNoContents into.

$PartNoContents = str_replace('"', '', $PartNoContents);
$PartLines = explode("\n", $PartNoContents);
$PartData = array();
foreach ($PartLines as $PartLine){
    $PartData[] = explode(',', $PartLine);
}

for($p=0;$p<count($PartLines);$p++){
   if ( isset( $PartData[$p][1] ) && !empty( $PartData[$p][1] ) ){
       $p1 = str_replace(chr(13), '', $PartData[$p][1]);
       if ( isset($BlockData[$b][0]) && !empty($BlockData[$b][0]) && $BlockData[$b][7]==$p1 ){
           $BlockData[$b][13] = str_replace(chr(13), '', $PartData[$p+$PartDataIncNum][1]);
           $p = count($PartLines) ;
       }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文