ted_chou12 Posted January 22, 2007 Share Posted January 22, 2007 [code]<?php$array = file("posts.txt");foreach ($array as $line) {$data = explode("#", $line);$array1[] = "".$data[0]." => ".$data[1]."";}foreach ($array1 as $line2) {echo $line2;echo "<br>";}print_r(array_keys($array1, "test"));?>[/code]the array_keys at the end does not echo out any thing at all despite i do have match string, what i believe is the problem is that "".$data[0]." => ".$data[1].""; isnt a proper array symbol, therefore cannot be recognized, any suggestions please?ThanksTed Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 It would be $data[0]=>$data[1];No string stuff. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 22, 2007 Author Share Posted January 22, 2007 if this:[code]<?php$array = file("posts.txt");foreach ($array as $line) {$data = explode("#", $line);$array1[] = $data[0]=>$data[1];}foreach ($array1 as $line2) {echo $line2;echo "<br>";}print_r(array_keys($array1, "test"));?>[/code]is what you mean, then it doesnt work, this problem:Parse error: syntax error, unexpected T_DOUBLE_ARROWshowsThanksTed Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 Oh right, just do =, I read too quickly. => is for when inside of the array() constructor.So I think you want$array1[] = array($data[0]=>$data[1]); Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 22, 2007 Author Share Posted January 22, 2007 thanks, but that resulted in:ArrayArrayArrayArrayArrayArrayArrayArrayArray ( ) and what i believe occured is that the array became a two dimensional array....Ted Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 22, 2007 Share Posted January 22, 2007 What are you attempting to accomplish with this code?Ken Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 22, 2007 Author Share Posted January 22, 2007 this is my txt file:[code]1#test2#aaa3#aaa4#bvbb5#avaer6#page7#test8#test.comg[/code]I am trying to find the linenumber for a certain string specified. i used test as an eg. but what i really wish to do is to used the id no. since it is unique and does not repeat it self, however, sometimes lines of the txt files can get modified or deleted, therefore:[code]1#test3#aaa4#bvbb7#test8#test.comg[/code]and the id no. is not neccessary related to the line number any more, therefore, i am trying create a code that finds out the line number for a specified id.ThanksTed Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 Yeah I'm confused too. I corrected the syntax, but I don't get what you're trying to do. Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 22, 2007 Share Posted January 22, 2007 Is this what you mean?[code]<?php$array = array("posts.txt");foreach ($array as $line) {echo $line;}?>[/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 So you want to put each line into an array?I think you want$array1[$data[0]] = $data[1]; Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 22, 2007 Author Share Posted January 22, 2007 thanks,not actually,what I want to do is when i give id 8, for example, and i wnat to get the line number of id 8:[code]1#test3#aaa4#bvbb7#test8#test.comg[/code]therefore, the code will return 4 for line 5.Ted Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 What you just said makes no sense. As always. Good luck. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 22, 2007 Author Share Posted January 22, 2007 what do you mean by makes no sense?I can understand it perfectly with no problem. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 "what I want to do is when i give id 8, for example, and i wnat to get the line number of id 8:therefore, the code will return 4 for line 5."You want the line number for line 5 but it should return 4? the 4 on there is line 3, line 5 is 8. If you understood that perfectly, that explains why you're having trouble with your code.Try the bit I wrote. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 22, 2007 Author Share Posted January 22, 2007 dont you know that php code starts with 0? and from the code above you can clearly see that 8 is in line 5, or is it the problem with your eyesight? Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 22, 2007 Share Posted January 22, 2007 does this work i am only seeing if it works someone tell me learning?[code]<?php$array = file("posts.txt");foreach ($array as $line) {$show="glob($line)";echo "$show<br>";}?>[/code] Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 22, 2007 Author Share Posted January 22, 2007 thanks, ill have a try :D Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 [quote author=ted_chou12 link=topic=123506.msg510547#msg510547 date=1169479801]dont you know that php code starts with 0? and from the code above you can clearly see that 8 is in line 5, or is it the problem with your eyesight?[/quote]ORLY? PHP STARTZ AT ZEROES? OMG!Yes, ID 8 is line 5. But as you yourself said, "therefore, the code will return 4 for line 5."Where the hell are you getting a four? Maybe if you would explain things in a way that made sense, you'd get more help.Did you TRY the flipping code I posted?[code]<?php$array = file("posts.txt");foreach ($array as $line) { $data = explode("#", $line); $array1[$data[0]] = $data[1];}foreach ($array1 as $line2) { echo $line2;echo "<br>";}print_r($array1);?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 22, 2007 Share Posted January 22, 2007 sorry i ment this [code]<?php$array = file("posts.txt");foreach ($array as $line) {$show=glob($line);echo "$show<br>";}?>[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 22, 2007 Share Posted January 22, 2007 jesirose can my example work as foreach and glob cheers? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 I don't think so, since it's not working with the directory. From what the manual says, glob isn't the right function to use here. I think explode was fine, but the problem is getting it into the array, which if he tries the code I posted, I think I fixed that. Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 22, 2007 Share Posted January 22, 2007 ok thank you for the reply cheers.try jesirose code Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.