/    Sign up×
Community /Pin to ProfileBookmark

Arrays – finding and grouping by unique values to later output.

This is taking a previous question of mine one step further, and it’s driving me nuts. I want to add this additional functionality, but am having a bear of a time getting my arrays correct. Here is what I have already:

[CODE]
<?php
$lines = array(
‘appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1),primary,e(1)’
‘appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2),primary,e(1)’
‘appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) primary,e(1)’
‘appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9),e(1)’
‘appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1),e(2)’
‘appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2),e(3)’
‘appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3),e(3)’
‘appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1),e(2)’
‘appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2),e(4)’
‘appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3),e(3)’
‘appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9),e(1)’
);
$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]

This currently outputs

[CODE]
2.4.1:
appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1),primary,e(1)
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1),e(2)
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1),e(2)
2.4.2:
appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2),primary,e(1)
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2),e(3)
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2),e(4)
2.4.3:
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) primary,e(1)
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3),e(3)
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3),e(3)
2.0.9:
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9),e(1)
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9),e(1)
[/CODE]

But I want to display:

[CODE]
2.4.1:
e(1):
appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1),primary,e(1)
e(2):
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1),e(2)
appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1),e(2)
2.4.2:
e(1):
appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2),primary,e(1)
e(3):
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2),e(3)
e(4):
appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2),e(4)
2.4.3:
e(1):
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) primary,e(1)
e(3):
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3),e(3)
appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3),e(3)
2.0.9:
e(1):
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9),e(1)
appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9),e(1)
[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 19.2011 — [code=php]
<?php
$lines = array(
'appname-2.4.1 2011/01/19 root /home/app/bin v(2.4.1),primary,e(1)',
'appname-2.4.2 2011/01/19 root /home/app/bin v(2.4.2),primary,e(1)',
'appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3) primary,e(1)',
'appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9),e(1)',
'appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1),e(2)',
'appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2),e(3)',
'appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3),e(3)',
'appname-2.4.1 2010/12/02 root /home/app/bin v(2.4.1),e(2)',
'appname-2.4.2 2011/01/13 root /home/app/bin v(2.4.2),e(4)',
'appname-2.4.3 2011/02/22 root /home/app/bin v(2.4.3),e(3)',
'appname-2.0.9 2010/01/21 root /home/app/bin v(2.0.9),e(1)'
);
$data = array();
foreach($lines as $line) {
if(preg_match('/^.*v((.*?)).*(e(d+))$/', $line, $matches)) {
$data[$matches[1]][$matches[2]][] = $line;
}
}
echo "<ul>n";
foreach($data as $key => $arr) {
echo "<li>$key:<ul>n";
foreach($arr as $key2 => $arr2) {
echo "<li>$key2<ul>n";
foreach($arr2 as $line) {
echo "<li>$line</li>n";
}
echo "</ul></li>n";
}
echo "</ul></li>n";
}
echo "</ul>n";
[/code]
Copy linkTweet thisAlerts:
@mcruauthorApr 20.2011 — Excellent. Again, much more efficient than what I was trying to do - I was doing another preg_match for the env as I looped through the first dataset that grouped by v(*).

I made a quick change to the regex pattern since I was missing some results that didn't have e(*) at the end of the string, or that had any space or single quotes (though I should probably be stripping out the single quotes and trimming any white space).

[CODE]
preg_match('/^.*v((.*?)).*(e(.*?))/', $line, $matches)
[/CODE]
×

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 12.9,
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: @bahaedd,
tipped: article
amount: 1000 SATS,

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

tipper: @mbsaad,
tipped: article
amount: 1000 SATS,
)...