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
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
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
"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 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]
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.