php从输入文件中选择特定内容放入数组元素
我有一个输入文件(从如下所示的文件中执行),其中包含多行,我需要从中选择特定文本并将每个选择放入数组元素中:
从输入文件中执行:
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该完成基本工作。阅读一行,将其分解为逗号出现的部分。第一部分将是“键”,第二部分将是值。然后继续阅读,直到我们点击 NAME 或 PARTSHAPE 键,然后根据需要存储值。
请注意,我没有去掉值中的双引号。这留给读者作为练习。此代码还假设文件的格式是常规的,并且“NAME”将显示在任何 PARTSHAPE 行之前,并且 NAME/PARTSHAPE 行之间将存在完美的 1:1 交替。如果您连续获得两个 PARTSHAPES,您将失去第一个。如果 PARTSHAPE 在第一个 NAME 出现之前出现,那么您也会丢失该名称。
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.
以下步骤对我有用:
粘贴在我的OP中的部分(重复多次)被定义为
$PartNoContents
$BlockData[]
是我需要将 $PartNoContents 中的选择粘贴到的数组。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.