1

i have a logfile which is saved as

{time} | {name} | {value1}
{time} | {name} | {value2}
{time} | {name2} | {value3}
{time} | {name3} | {value4}

the {time} is a timestamp in "Seconds since the Unix Epoch"

and the logfile is written line by line so the latest log line is at the bottom,

and i am trying to categorize the values into a php array....

lets say we have

{time} | Steve | Pizza
{time} | Steve | Kebab
{time} | Steve | Burger
{time} | John | Kebab
{time} | John | Ice-Cream
{time} | Dave | Pizza
{time} | Derek | Ice-Cream
{time} | Derek | Fanta

i have exploded into array $lines

this is what i can use to get the variables....

echo $lines[0][1] . "," . $lines[0][2];  // this will print ' Steve,Pizza '
echo $lines[3][2];  // this will print ' Kebab '

i am trying to somehow code an array using ' $lines[$x][1] ' as the array keys and ' $lines[$x][2] ' as the array values so i can call:

print_r($new_array);

to get:

array
     (
  [Steve] => Pizza
          => Kebab
          => Burger
     )
     (
  [John] => Kebab
         => Ice-Cream
     )
     (
  [Dave] => Pizza
     )
     (
  [Derek] => Ice-Cream
          => Fanta
     )

the reason for doing this is i need to display a small table which will look like this ....

 _________________________________________
|       Steve       |      Pizza          |
|                   |      Kebab          |
|___________________|_____ Burger ________|
|       John        |      Kebab          |
|___________________|_____ Ice-Cream _____|
|______ Dave _______|_____ Pizza _________|
|       Derek       |      Ice-Cream      |
|___________________|_____ Fanta _________|

the array $lines is already formatted to remove all "log Lines" older than 24 hours....

so i dont think i will need to limit the amount of array keys {names}

thanks in advance for any suggestions.... have tried all logical solutions for this :rant:

DJ-P.I.M.P
  • 105
  • 2
  • 6
  • 15

2 Answers2

2

Didn't fully test, but try:

$new = array();
foreach($lines as $line)
{
    $new[$line[1]][] = $line[2];
}
print_r($new);

EDIT BY DJ-P.I.M.P...

$new = array();
foreach($lines as $line)
{
$new[$line[1]][] = $line[2];
}
// print_r($new);


foreach($new as $key => $news) {

echo $key . "<br>";

foreach($news as $k => $v) {

echo $news[$k] . "<br>";

}
}

This does exacly as my question was asking .... Kudos, for the initial idea @ Aaron :)

DJ-P.I.M.P
  • 105
  • 2
  • 6
  • 15
Aaron W.
  • 9,254
  • 2
  • 34
  • 45
  • ok this works perfectly but i cannot get the syntax correct to print / echo {name1} and {values}...... is it $new[$line][0] for name and $new[$line][1] for values? – DJ-P.I.M.P Feb 13 '12 at 20:08
  • im now thinking it may be another foreach on $new .... so that i can list $name[$x] and $value[$x][1]..... and $name[$y] and $value[$y][1] ? – DJ-P.I.M.P Feb 13 '12 at 20:14
  • ok im halfway there, i have foreach($new as $key => $news) { echo $key . "
    "; print_r($news); echo "
    "; } ..... this shows me its working as expected..... now i write new foreach nested to display each value aswell thanks
    – DJ-P.I.M.P Feb 13 '12 at 20:29
  • Awesome glad it works for you. Wasn't able to check back but good work – Aaron W. Feb 13 '12 at 22:58
0

Different variation to add values sharing the same key in sub arrays:

$result = array();
for ($lines as $line) {
    if (!isset($result[$line[1]])) {
        $result[$line[1]] = array();
    }
    $result[$line[1]][] = $line[2];
}
Feysal
  • 623
  • 4
  • 7