Jump to content

What have i done wrong (double arrow array)


ted_chou12

Recommended Posts

[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?
Thanks
Ted
Link to comment
Share on other sites

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_ARROW
shows
Thanks
Ted
Link to comment
Share on other sites

this is my txt file:
[code]
1#test
2#aaa
3#aaa
4#bvbb
5#avaer
6#page
7#test
8#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#test
3#aaa
4#bvbb
7#test
8#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.
Thanks
Ted
Link to comment
Share on other sites

"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.
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.