TonyYayo Posted November 11, 2007 Share Posted November 11, 2007 First of all, Im sorry if something similar has been posted somewhere on the forum, just started with PHP and thus I do not know where to begin searching since im not completely sure what Im looking for. My problem is as follow: I want to search through an array, and post each element in a table. This array contains alot of elements with the same value, and instead of presenting these repeatedly in my table, I want a row with a number representing how many times the value is in the array. I know this can be solved with sorting the array and creating some kind of counter that adds up if the element is similar to the previous, but I just can't wrap my head around the syntax. Any help would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/76835-solved-beginner-question-regarding-arrays/ Share on other sites More sharing options...
MadTechie Posted November 11, 2007 Share Posted November 11, 2007 may i be the first to say welcome to the phpfreak forum as for your problem i would do something like this.. <?php $theArray = array("bob", "John", "bob", "Tim", "Bob", "John"); $newArray = array(); sort($theArray); //Not need in this code (but you said about sorting) $last=""; foreach($theArray as $e) { #$e = strtolower($e); // uncomment to ignore case if(array_key_exists($e, $newArray)) { $newArray[$e] = $newArray[$e] + 1; //Note you can also do $newArray[$e]++ }else{ $newArray[$e] = 1; } } foreach($newArray as $C => $Names) { echo "$Names = $C times<br>"; } ?> Note that theirs bob an Bob, which will count as 2 different elements unless you remove the # from this line #$e = strtolower($e); // uncomment to ignore case of course your need to format this for a table.. EDIT: added comments Quote Link to comment https://forums.phpfreaks.com/topic/76835-solved-beginner-question-regarding-arrays/#findComment-389006 Share on other sites More sharing options...
TonyYayo Posted November 11, 2007 Author Share Posted November 11, 2007 Thanks alot. Quick respond, and pretty much what I was looking for. Now I just need to adapt this to my context, but I'll think I can manage that without loosing to much hair. Need to import strings from a *.txt, namely IP-adresses (logging traffic) so upper vs. lower case won't be an issue, but it's always good to know for future tasks. Again, thanks for the help. Marking this topic as solved, but If anyone have some tips regarding logs and similar your welcome //Tony Quote Link to comment https://forums.phpfreaks.com/topic/76835-solved-beginner-question-regarding-arrays/#findComment-389018 Share on other sites More sharing options...
MadTechie Posted November 11, 2007 Share Posted November 11, 2007 if your reading in line by line from a text file you could do something like this <?php $newArray = array(); $handle = fopen("IP.txt", "r"); if ($handle) { while (!feof($handle)) { $e = fgets($handle); //Readline $e = trim($e); //clear and spaces //Check its a valid IP (the whole sring) if (preg_match('/^(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $e)) { if(array_key_exists($e, $newArray)) { $newArray[$e]++; }else{ $newArray[$e] = 1; } } } fclose($handle); } foreach($newArray as $C => $Names) { echo "$Names = $C times<br>"; } ?> I added a RegEx for checking valid IP's (you may wany to remove it) Quote Link to comment https://forums.phpfreaks.com/topic/76835-solved-beginner-question-regarding-arrays/#findComment-389028 Share on other sites More sharing options...
TonyYayo Posted November 11, 2007 Author Share Posted November 11, 2007 Thanks again. Even tho I was kinda lost a few hours ago, adapting these solutions successfully to my script gives me a somewhat feeling of accomplishment. The difference between strugling and actually wanna learn more is short See ya around Quote Link to comment https://forums.phpfreaks.com/topic/76835-solved-beginner-question-regarding-arrays/#findComment-389034 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.