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);
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
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.