Jump to content

[SOLVED] Format an array


php_dave

Recommended Posts

Hi all,

 

Im struggling with something probably really easy!! but just cant work it out!

 

I have an array that is populated like so

 

1, 1, 2007-08-16, 1, 2, 1
2, 1, 2007-08-16, 2, 2, 1
3, 1, 2007-08-16, 5, 6, 1
4, 2, 2007-08-16, 1, 3, 1
5, 2, 2007-08-16, 1, 5, 6 

 

im trying cycle through this array and use the 2nd value as a group or header like below

 

1:

  1, 2, 1

  2, 2, 1

  5, 6, 1

2:

  1, 3, 1

  1, 5, 6

 

I cant get close - any ideas?

 

Cheers

Dave

 

Link to comment
https://forums.phpfreaks.com/topic/65644-solved-format-an-array/
Share on other sites

<?php

$currentHeader = '';

foreach ($array as $valueArray) {

  if ($currentHeader != $valueArray[1]) {
    echo '<b>' . $valueArray[1] . ':</b><br>';
    $currentHeader = $valueArray[1];
  }

  echo $valueArray[3] . ', ' . $valueArray[4] .', '. $valueArray[5] . '<br>';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.