Help - Search - Members - Calendar
Full Version: multi level arrays in php
Invision Power Services > Community Forums > Community Web Design and Coding
Johnny Bravo
How can I read line 1 array element 5 of an array with this code?
All I get is every 5th element of each line in the file

if (file_exists($readname))
{
$getdata=fopen($readname,"rb");
while (!feof($getdata))
{
$readdata=fgets($getdata, 999);
$line = explode( "*", $readdata);

echo "$line[5],";
Chad
What is the array you are trying to read?
Post array as well.

Chad
Johnny Bravo
Hi all I have a file that I need to open and get a single element from. I thought I would make the file into an array, in hopes that each element would then be callable as a varible. This did not work but I am unsure of correct syntax for callign a multi level array? I am finding this to be harder than I thought. Here is what the file looks like.


ISA*00* *00* *01*043109560T *01*3125533 *050209*1057*U*00401*000000045*0*T*>~
GS*PR*043109560T*3125533*20050209*1057*15*X*004010~
Pattern matched: GS THIS line contains the UNique ID Number I needST*855*000000022~
BAK*00*AE*TEST01*20050201~
N1*ST*JOHN UTTER~
N3*1415 34TH~
N4*FARGO*ND*58103~
PO1*1*1*EA*****VN*PXX X879512X~
PID*F****1 LOVE (CHECKPOINT)(DVD)(~

I need to get element 1057 of the GS line. sad.gif
Can anyone help me? I am lost!
Chad
Is this the only line you require?
Or does it change from time to time?

If this is the only string you require permanetly, then there is an easier solution.

Chad
Johnny Bravo
QUOTE(Chad @ Mar 15 2005, 09:33 PM)
Is this the only line you require?
Or does it change from time to time?

If this is the only string you require permanetly, then there is an easier solution.

Chad
*


Hey dude,
The line data itself with be diffrent every time. The format will be the same for the most part unless the number grows so that it needs another field for a roll over..ie 9 becomes 10. But that is the only line I need data from. Line 1 if you started an array at 0. And that element. For some reason I cant just put it as a File and call an element. ???
Johnny Bravo
I got it to work, since it was so hard for me I thought I would try and post the code that finally broke the hump! i hope someone can use this!

In PHP here is the winning code

echo "This is a number number array of the order 855 receipt";
echo "<br>";
$lines=file("X12.997.20050202.083929.txt");
foreach ($lines as $line_num => $line)//not needed but nice
$line2 = explode( "*",$line);// also called twice but I like it here too

echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
sort($lines);
$line2 = explode( "*",$lines[6]);
echo $line2[5];
echo "<br>";

my code, Its a little sloppy but It helped me to see whats going on from a few angles.
The kicker or winner is the sort command. So I will do my best to describe what is happening.
I open a file with $lines=file()
The File command puts everything in the () into an array.

My file is deliminated by * so I needed to find a way to seperate each line by the deliminator. I also needed a way to get to that line of the array since the file was multilined. Here is what the a good portion of the txt file actully looks like when its opened.

ISA*00* *00* *01*043109560T *01*3125533 *050209*1057*U*00401*000000045*0*T*>~
GS*PR*043109560T*3125533*20050209*1057*15*X*004010~
Pattern matched: GS THIS line contains the UNique ID Number I needST*855*000000022~
BAK*00*AE*TEST01*20050201~
N1*ST*JOHN~
N3*1415 34TH~
N4*FARGO*ND*58103~
PO1*1*1*EA*****VN*PXX X879512X~

Using sort it sorted the array or the file alphabetical by the first character of the array element. so by calling or echoing $lines[6] it displayed all of line GS*PR etc..etc.. becuse G was the 6th letter in the overall sort. So then I broke up that line by the deliminator of * with the explode command and now can grab each and every stinking little element. RRRRRRRRRRRRRRRRRRRRRRR

Thanks all stay cool and keep up the coding!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.