ted_chou12 Posted January 23, 2007 Share Posted January 23, 2007 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?ThanksTed Link to comment https://forums.phpfreaks.com/topic/35364-array_keys/ Share on other sites More sharing options...
Orio Posted January 23, 2007 Share Posted January 23, 2007 You cant make array_keys return a string/int instead of an array.Can you tell us what you are trying to do exactly?Orio. Link to comment https://forums.phpfreaks.com/topic/35364-array_keys/#findComment-167125 Share on other sites More sharing options...
ted_chou12 Posted January 23, 2007 Author Share Posted January 23, 2007 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#test2#aaa//line 23#aaa4#bvbb5#avaer6#page7#blue8#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 https://forums.phpfreaks.com/topic/35364-array_keys/#findComment-167131 Share on other sites More sharing options...
Orio Posted January 23, 2007 Share Posted January 23, 2007 I still don't understand what's the goal. What are you try to achieve? Maybe there's a simple way doing it...Orio. Link to comment https://forums.phpfreaks.com/topic/35364-array_keys/#findComment-167135 Share on other sites More sharing options...
HuggieBear Posted January 23, 2007 Share Posted January 23, 2007 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] :-\RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/35364-array_keys/#findComment-167136 Share on other sites More sharing options...
ted_chou12 Posted January 23, 2007 Author Share Posted January 23, 2007 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.ThanksHope that makes it clearTed Link to comment https://forums.phpfreaks.com/topic/35364-array_keys/#findComment-167148 Share on other sites More sharing options...
HuggieBear Posted January 23, 2007 Share Posted January 23, 2007 OK, try this...[code]<?php$array = file("posts.txt");foreach ($array as $line) { list($k, $v) = explode("#", $line); $array1[$k] = $v;}print_r(array_keys($array1, "2"));?>[/code]RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/35364-array_keys/#findComment-167170 Share on other sites More sharing options...
boo_lolly Posted January 23, 2007 Share Posted January 23, 2007 [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?ThanksTed[/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 https://forums.phpfreaks.com/topic/35364-array_keys/#findComment-167172 Share on other sites More sharing options...
ted_chou12 Posted January 23, 2007 Author Share Posted January 23, 2007 thanks Link to comment https://forums.phpfreaks.com/topic/35364-array_keys/#findComment-167199 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.