Jump to content

[SOLVED] Beginner question regarding arrays


TonyYayo

Recommended Posts

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

 

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

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.