Page 1 of 1

[SOLVED] Array Woes

Posted: Sun Jun 26, 2005 8:57 pm
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.

Posted: Sun Jun 26, 2005 9:11 pm
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.

Posted: Sun Jun 26, 2005 9:18 pm
by infolock
Thanks todd, i'll try that. i'll post what i find :) Thanks again

Posted: Sun Jun 26, 2005 9:49 pm
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;
    }
  }
}