rhyspaterson Posted May 28, 2007 Share Posted May 28, 2007 Hey guys, This is the first time i have needed to search through an array before. My .txt file that is converted into an array looks like this. <bt>600</bt> <iregno>123123</iregno> <users> <un>2</un> <u1> <n>Bob</n> <ema>bob@domain.com<ema> <s>bobspassword</s> <t>admin</t> <p>Bob's Profile</p> <cat>101010000000000000000000000000100</cat> <u2> <n>Fred</n> <ema>fred@domain.com<ema> <s>fredspassword</s> <t>user</t> <p>Fred's Profile</p> <cat>000100000001000100000000110000000</cat> // unlimited users can go here: u3, u4, ux ect... </users> In my array, each line is a separate entity (i.e array[0] is <bt>600</bt> and array[1] is <iregno>123123</iregno>). However the file can have an unlimited amount of users. Finding out how many users there are is easy, because i just look at the output of $array[4] (<un>2</un> - there are 2 users). However i need to then search for and list each users name (<n>name></n>), or store it in another array, depending on how many users there are (u1, u2, uX) and just not quite sure how to go about this.. Any pointers would be muchly appreciated /Rhys Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted May 28, 2007 Share Posted May 28, 2007 Since it is occuring in regular intervals I would do something like this //Code to find out how many users are in the array $i = 0; $b = 0; $people = array(); while($i <= $totalusers){ array_push($people, $bigarray[$b]); $b = $b + $interval; } I could be getting it all wrong of course. But if the array has a set interval between each name you can just pull those values out of the array and then push them onto the other array. Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted May 28, 2007 Author Share Posted May 28, 2007 Thanks for the reply. Unfortunately there is not a set interval between the users. I should have made that clearer before. There will be other 'lines' in each user section depending on how many things they choose. I guess what i'm looking for then is a way to search an array for a string that matches "<u>USERNAME</u>", where USERNAME can be anything. in logical terms it could be $userCount = 4; (which i know) search $bigArray from the start and find "<u>" . "USERNAME" . "</u>" and keep going until 4 results are returned. put these 4 results into another array. Any suggestions? Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted May 28, 2007 Share Posted May 28, 2007 Ok well in that case maybe make it so something is stored before every username in the array, then use array_keys. For example: If you had it so that the word "Wolf" was placed in the array key before every username then you could theoretically do this: $keyarray = array_keys($array, "Wolf") //Returns an array with all the key numbers of the Wolfs $i = 0; while($i <= $usernumber){ $t = $keyarray[$i] + 1; $name = $array[$t]; array_push($namearray, $name); } So thus with a hierarchy like this in your array: Array0 = Something Array1 = Wolf Array2 = Username Since the Wolf is always 1 key before the username, when you add 1 to the keys you pulled and then draw out of the big array you would theoretically always get the username. Of course it could be anything before the username key, as long as its constant. Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted May 28, 2007 Author Share Posted May 28, 2007 Sorry, Realized i needed to put this in code brackets. $userCount = 4; (which i know) search $bigArray from the start and find "<n>" . "USERNAME" . "</n>" and keep going until 4 results are returned. put these 4 results into another array. Where USERNAME can be anything Now i ran a little test code of array_keys, and here is what i found. If i <b>know</b> bob is one of the user names, then technically this code should output.. something. print_r(array_keys($arrayTextPlain, "bob")); Which it doesn't. Confused. Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted May 28, 2007 Share Posted May 28, 2007 Are the usernames all gonna be different? Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted May 28, 2007 Author Share Posted May 28, 2007 Yes each username will be different. They will look like this <n>Jack White</n> Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted May 28, 2007 Share Posted May 28, 2007 Will the <n> part be unique to only the username? There wont be any more <n> preceding another array element? Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted May 28, 2007 Author Share Posted May 28, 2007 No the <n>USERNAME</n> part is unique to the username. There will however be other usernames further down the array using the <n></n> tags. Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted May 28, 2007 Share Posted May 28, 2007 Yea thats what I meant. In that case do something like this perhaps? foreach($bigarray as $t){ if(substr($t, 0, 2) == "<n>"){ $takeout = array("<n>", "</n>"); $name = str_replace($takeout, "", $t); array_push($namearray, $name); } } My foreach skills are very rusty since I never use them, so it may need some refining. Basically all that does is check each array element in $bigarray's first 3 letters to see if they are <n>. If they are then it will strip out the <n> and the </n> and push just the username into the name array. If not, it will just move onto the next array element. Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted May 28, 2007 Author Share Posted May 28, 2007 Perfect! That's exactly what i need. However running the code returns NULL. When it should return Bob, Fred, Sam, Bill and Barry. $array = array("<n>Bob</n>", "<n>Fred</n>", "<n>Sam</n>", "<n>Bill</n>", "<n>Barry</n>"); foreach($array as $t){ if(substr($t, 0, 2) == "<n>"){ $takeout = array("<n>", "</n>"); $name = str_replace($takeout, "", $t); array_push($namearray, $name); } } echo "PRINTING NAMEARRAY<br /><br />"; var_dump($namearray); Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted May 28, 2007 Author Share Posted May 28, 2007 / removed double post Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted May 28, 2007 Share Posted May 28, 2007 Something very odd is happening that may throw it off. When I ran this code on my server: <?php $bigarray = array(0 => '<n>blue</n>', 1 => 'red', 2 => '<n>green</n>', 3 => 'red'); $namearray = array(); foreach($bigarray as $t){ if(substr($t, 0, 2) == "<n>"){ $takeout = array("<n>", "</n>"); $name = str_replace($takeout, "", $t); array_push($namearray, $name); } } ?> I noticed that it stripped out the <n>. Infact the whole < > thing seemed to screw up the array. I dont know why this is. Its most likely my the browser trying to do some HTML stuff with it when printing out. I have to go to bed soon, since its 1:20, so I am gonna have to leave ya to figuring it out. I hope what I put in helped, and you can figure it out. That should hypothetically be all you need. Best of luck ~StormTheGates Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted May 28, 2007 Author Share Posted May 28, 2007 Thanks muchly for your help. I have been thinking HTML has some kind of effect too. Anyone have any suggestions to the above code? Quote Link to comment Share on other sites More sharing options...
StormTheGates Posted May 28, 2007 Share Posted May 28, 2007 I decided I would rather help you out than sleep. I also figured out the solution <?php $bigarray = array('<n>blue</n>', 'red', '<n>green</n>', 'red'); $namearray = array(); foreach($bigarray as $t){ if(substr(htmlspecialchars($t), 0, 2) == '&l'){ $takeout = array("&l"); $name = str_replace($takeout, "", $t); array_push($namearray, $name); } } ?> You see I encoded the HTML shit, and then &l which was the representation of <n>. So huzzah it displayed fine for me in my web browser with the print_r command. Hope it works for you Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted May 28, 2007 Author Share Posted May 28, 2007 Thank you very much Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2007 Share Posted May 28, 2007 if you treat the file as one long text string instead of an array of lines <?php $text = file_get_contents('myfile.txt'); $p1 = $p2 = 0; $names = array(); $L = strlen($text); while ($p1 < $L) { if (($p1 = strpos($text, '<n>', $p2))===false) break; $p1 += 3; $p2 = strpos($text,'</n>', $p1); $names[] = substr($text, $p1, $p2-$p1); } echo '<pre>', print_r($names, true), '</pre>'; ?> 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.