Jump to content

array_keys


ted_chou12

Recommended Posts

this is my whole code:
[code]
<?php
$array = file("posts.txt");
foreach ($array as $line) {
$data = explode("#", $line);
$array1[] = $data[0];}
print_r(array_keys($array1, "2"));?>
[/code]
the text file:
[code]
1#test
2#aaa//line 2
3#aaa
4#bvbb
5#avaer
6#page
7#blue
8#test.comg
[/code]
This gives the line number of the txt file, in my eg. i used 2 so i want it to echo 1 as in line 2
Link to comment
Share on other sites

Ted, remember what I advised last time?  Take a little time explaining your posts well and you're bound to get a better response.

The following doesn't make sense, are you sure you haven't missed off the middle of the sentence?

[quote]This gives the line number of the txt file, in my eg. i used 2 so i want it to echo 1 as in line 2[/quote]  :-\

Regards
Huggie
Link to comment
Share on other sites

Introduction: What I mainly want to do is to search for a match string in the text file, however, more than that, I also want to know the line number (position) of the match string. str_pos wouldnt satisfy that because it only gives the relative position of the letter/symbol, and does not take line number into account. thorpe suggest me to use array_keys.
each line of the txt file has a number infront, thats the id, and because the id is not repeated, therefore I want to use it to find the line number that the entry is in.
Thanks
Hope that makes it clear
Ted
Link to comment
Share on other sites

[quote author=ted_chou12 link=topic=123658.msg511310#msg511310 date=1169562185]
the result of this function gives:
[code]
Array ( [0] => 1 )
[/code]
where 0 represents the result number, and 1 represents the number of the element in the array, what would i have to do to just get the value "1" instead of a whole Array formation result?
Thanks
Ted
[/quote]
i think this is what you want:
[code]
<?php
$array = array("entry 1", "entry 2",
      "entry 3", "entry 4");

foreach($array as $key => $val){
echo "val: ". $val ."<br />\n";
}
?>
[/code]
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.