/    Sign up×
Community /Pin to ProfileBookmark

Array question – finding and displaying unique values

I’ve got an interesting problem with arrays in php. I’m totally new to php so bear with me…

I’ve creating an array based off lines returned from a log that resides on a server. I want to find unique values within a specific set of tags, and store those unique values. Then I want to loop through the entire array, and group it when any value in the array contains the unique values I had stored.

My exec() cmd:

[CODE]
$cmd=”<grabs contents of a log>”
exec($cmd, $output);
[/CODE]

Example of data in $output array returned from exec()
appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1), primary ‘
appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2), primary ‘
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3), primary ‘
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9) ‘
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1) ‘
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2) ‘
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) ‘
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1) ‘
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2) ‘
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) ‘
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9) ‘

From here, I want to loop through the array and store any values between v(). Then I want to store those in another array called $arrayUnique.

So $arrayUnique would contain:
2.0.9
2.4.1
2.4.2
2.4.3

Then I want to loop through $output again and display the all lines from $output that have a value in v() that matches the current value in $arrayUnique as I loop through arrayUnique.

example in psuedo code:

[CODE]
for n to upper bound of $arrayUnique
print “Unique Group:”;
for i to upper bound of $output
if ( n is a substring in string i)
print i;
end if
next
next
[/CODE]

So I would be left with:
Unique Group:
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9) ‘
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9) ‘

Unique Group:
appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1), primary ‘
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1) ‘
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1) ‘

Unique Group:
appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2), primary ‘
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2) ‘
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2) ‘

Unique Group:
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3), primary ‘
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) ‘
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) ‘

Thanks, sorry this is so long. And I know I missed a few line breaks in the loop for the output to look correct, but you get the idea.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 07.2011 — Are you looking for something like this?
[code=php]
<?php
$lines = array(
'appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1),primary',
'appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2),primary ',
'appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) primary ',
'appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9)',
'appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1)',
'appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2)',
'appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3)',
'appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1)',
'appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2)',
'appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3)',
'appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9)'
);
$data = array();
foreach($lines as $line) {
if(preg_match('/^.*v((.*?)).*$/', $line, $matches)) {
$data[$matches[1]][] = $line;
}
}
echo "<ul>n";
foreach($data as $key => $value) {
echo "<li>$key:<ul>n";
foreach($value as $line) {
echo "<li>$line</li>n";
}
echo "</ul>n";
}
echo "</ul>n";
[/code]

Output:
<i>
</i> 2.4.1:
appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1),primary
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1)
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1)
2.4.2:
appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2),primary
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2)
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2)
2.4.3:
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) primary
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3)
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3)
2.0.9:
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9)
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9)
Copy linkTweet thisAlerts:
@OctoberWindApr 07.2011 — This was actually kinda fun to figure out. (Nog beat me by minutes.. ? )

Set up some data:
[code=php]
$unique_version = array();
$tmpSourceArray = array(
"appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1), primary '",
"appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2), primary '",
"appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3), primary '",
"appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9) '",
"appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1) '",
"appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2) '",
"appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) '",
"appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1) '",
"appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2) '",
"appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) '",
"appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9) '");


// $tmpSourceArray would be $output
[/code]


Loop through each row, find the data between v(), and stuff it into a multidimentional array

[code=php]
unset($tmpLineNumber, $tmpLogData);
foreach ($tmpSourceArray as $tmpLineNumber => $tmpLogData)
{
preg_match('/v((.*))/i', $tmpLogData, $matches);
// $matches[0] returns the "search criteria"
// $matches[1] returns the first set of data matching the search
$unique_version[$matches[1]][$tmpLineNumber] = $tmpLogData;
}
[/code]


$unique_array is:
<i>
</i>Array
(
[2.4.1] =&gt; Array
(
[0] =&gt; appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1), primary '
[4] =&gt; appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1) '
[7] =&gt; appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1) '
)

<i> </i>[2.4.2] =&gt; Array
<i> </i> (
<i> </i> [1] =&gt; appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2), primary '
<i> </i> [5] =&gt; appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2) '
<i> </i> [8] =&gt; appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2) '
<i> </i> )

<i> </i>[2.4.3] =&gt; Array
<i> </i> (
<i> </i> [2] =&gt; appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3), primary '
<i> </i> [6] =&gt; appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) '
<i> </i> [9] =&gt; appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) '
<i> </i> )

<i> </i>[2.0.9] =&gt; Array
<i> </i> (
<i> </i> [3] =&gt; appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9) '
<i> </i> [10] =&gt; appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9) '
<i> </i> )

)


A less-than-favorable "double-foreach loop" to spit it out into a table.

[code=php]
echo "<table border='1'>";

unset($tmpUniqueKey, $tmpUniqueVal);
foreach($unique_version as $tmpUniqueKey => $tmpUniqueVal)
{
echo "t<tr><th colspan='2'>Unique Group for". $tmpUniqueKey .":</th></tr>n";
echo "t<tr><td>Line</td><td>Data</td></tr>n";
//kdo($tmpUniqueVal, "tmpUniqueVal");
unset($tmpValRow, $tmpValData);
foreach ($tmpUniqueVal as $tmpValRow => $tmpValData)
{
echo "t<tr><td>" . $tmpValRow . "</td><td>" . $tmpValData . "</td></tr>n";
}
}
echo "</table>";

[/code]
Copy linkTweet thisAlerts:
@mcruauthorApr 08.2011 — holy crap that is much better than what I came up with (just using php array functions I found on w3 schools). Thanks! I wanted to use regex but didn't realize php handled regex so well so I avoided it. Also, I've got to find a good IDE - any suggestions? I'm really used to Visual Studio (usually writing c#), but am just using notepad ++ for php now.

[CODE]
exec($cmd, $output);
$uniqueValsArray = array();
foreach($output as $client){
$singleClient = explode(" ", $client);
$version = $singleClient[7];
$version = str_ireplace(",","",$version);
$version = str_ireplace("v","",$version);
$version = str_ireplace("(","",$version);
$version = str_ireplace(")","",$version);

$inArray = true;
foreach ($uniqueValsArray as $val){
if ($version == $val)
$inArray = false;
}

array_push($uniqueValsArray, $version);
}

foreach ($uniqueValsArray as $uVal)
{
echo "Unique Group: <br />";
foreach($output as $client)
{
//echo $client;
if (strstr($client, $uVal) != false)
{
echo $client . "<br />";
}
}
}
[/CODE]


^ of course this had the problem of 2.1 matching 2.1.2 for example.
×

Success!

Help @mcru spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...