[SOLVED] Array Woes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

[SOLVED] Array Woes

Post by infolock »

Ok, I am having a problem with array data, and I think I'm just making it harder than it really is. I have an array with keys and values and valuevalues within each key.

I'm pulling this data from a mysql table and then creating the array based on the row data (ie :

Code: Select all

//....
while($row=mysql_fetch_assoc($sql) {
  $MyArray[] = $row;
}
//....
For instance, let's say that this is my array :

$MyArray

Code: Select all

Array
(
  ї0] => Array
    (
      їvehicle_type] => truck
      їmanufacturer] => Toyota
      їyear] => 2004
    )
  ї1] => Array
    (
      їvehicle_type] => Car
      їmanufacturer] => Nissan
      їyear] => 1998
    )
  ї2] => Array
    (
      їvehicle_type] => Car
      їmanufacturer] => Nissan
      їyear] => 2000
    )
  ї3] => Array
    (
      їvehicle_type] => Car
      їmanufacturer] => Ford
      їyear] => 2005
    )
)
notice how 1 and 2 have the exact same vehicle_type, and manufacturer. What I want to do is loop through the array, and find the vehicle type. And while the vehicle type is set to Car, loop search for Reoccuring Manufacturers, and then combine the years together.

So, the resulting array would look something like this :

Code: Select all

Array
(
  ї0] => Array
    (
      їvehicle_type] => truck
      їmanufacturer] => Toyota
      їyear] => 2004
    )
  ї1] => Array
    (
      їvehicle_type] => Car
      їmanufacturer] => Nissan
      їyear] => 1998, 2000
    )
  ї2] => Array
    (
      їvehicle_type] => Car
      їmanufacturer] => Ford
      їyear] => 2005
    )
)

See what I mean? I've busted my damn brain on trying to figure this out. Can anyone help out? Thanks.

Again, what i'm trying to accomplish is loop through the numerical index of the array (0,1,2,3,...). While i'm looping through the index, I want to say "Ok, if the next 5 indexes all have the EXACT SAME vehicle_type, then search to see if the MANUFACTURER is the EXACT same. If we Find duplicate manufacturers, then combine all their YEAR values into one big value seperated by commas, otherwise, continue with the script.
Last edited by infolock on Sun Jun 26, 2005 10:09 pm, edited 1 time in total.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Iterate through the array, and search for the same type then append to the year.

Code: Select all

function combineYears ( &$array ) {
  for ( $i = 0; $i < count($array); $i++ )
    for ( $j = i+1; $j < count($array); $j++ )
      if ( $array[$i]["type"] == $array[$j]["type"] && $array[$i]["manu"] == $array[$j]["manu"] ) {
        $array[$i]["year"] .= ", ".$array[$j]["year"];
        unset( $array[$j] );
      } else continue;
        // The previous line is to cause this for loop to break if the subsequent rows are not the same type and manufacturer
}
That code is untested, but you get the general principle - any rows after the current one that has the same type and manufacturer will have the year appended to the first instance in the array.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Thanks todd, i'll try that. i'll post what i find :) Thanks again
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

Todd, u are the freakin MAN ! god i would give anything to buy you a beer here.

Edit : i see you saw the [$j] instead of ['year'] in that unset too :) thanks again bro!! :-D


Edit2 : also, instead of continue, you should probably use break. otherwise it causes a 30 second php execution timeout.

Code: Select all

function combineYears ( &$array ) {
  for ( $i = 0; $i < count($array); $i++ ) {
    for ( $j = i+1; $j < count($array); $j++ ) {
      if($array[$i]["type"] == $array[$j]["type"] && $array[$i]["manu"] == $array[$j]["manu"]) {
        $array[$i]["year"] .= ", ".$array[$j]["year"];
        unset( $array[$j] );
      } else break;
    }
  }
}
Post Reply