Author Topic: Associative array help  (Read 404 times)

0 Members and 1 Guest are viewing this topic.

Offline zymurgyTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Associative array help
« on: November 13, 2006, 08:46:17 PM »
Hello,

After reading in a lot of random data from a database and storing it all in an array. The way it looks is something like:

Dealer number, Dealer name, Car Color, Body Type, etc etc etc

There are a variable number of different unique dealer numbers, but many of the cars belong to the same dealers. Example

Car 1: 6145, Foo Auto, Red, Sedan, etc etc etc
Car 2: 4538, Bar Auto, Blue, Truck, etc etc etc
Car 3: 6145, Foo Auto, Black, Van, etc etc etc
Car 4: 5840, Hello World, White, Sedan, etc etc etc
Car 5: 4538, Bar Auto, Yellow, Submarine, etc etc etc

Those 5 cars are all in an array. The array elements are just bar delimited string (6145|Foo Auto|Red|Sedan|etc etc etc).

What I need to do is parse this data in such a way where I have all the cars for each dealer in a neat manner. Here is where my questions come in.

1: The only way I could think of doing it was an associative array where the key is the dealer number, and the value associated with that key is an array of cars. Obvious question here is if there is a simpler way, I can't think of one.

2: Trying to put my idea into code didn't go too well for me...

Code: [Select]
$formatData = array();
foreach ($allCars as $_count => $_car) {
$flag = 0;
foreach ($formatData as $key => $value)
if ($_car['DealerNum'] == $key)
$flag = 1;
if (!$flag) //key is not in the array

I couldn't really get much farther than that though I don't even think that much is right.

Any help would be greatly appreciated, thank you very much.

Offline ToonMariner

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
Re: Associative array help
« Reply #1 on: November 13, 2006, 08:54:05 PM »
bar delimited is not an array its a string.

If you are retieving form a database try this...

Code: [Select]
<?php
$qry 
"SELECT * FROM `carstable` ORDER BY `dealer_name` ASC";
$qry mysql_query($qry);
$cararr = array();
while(
$row mysql_fetch_assoc($qry))
{
 foreach(
$row as $key => $value)
 {
  
$cararr[$key][] = $val;
 }
}
?>


This will create an associative array of information from your database.

THEN you can use functions like array_keys to pull certain things (like every red vehicle) or array_multisort to alter the order.....


If you need any more help just ask...
follow me on twitter @PHPsycho

Offline zymurgyTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: Associative array help
« Reply #2 on: November 13, 2006, 08:56:11 PM »
The string isn't an array, the array's elements are strings which contain information about the cars in a bar delimited fashion.

Unfortunately, given the way the databases are set up and the way the data needs to be formatted to be transmitted to the server, using the given code will not accomplish what I need. I need to work with the array I have.

Array1:

Element 1: "6145|Foo Auto|Red|Sedan|etc etc etc"
Element 2: "4538|Bar Auto|Blue|Truck|etc etc etc"
etc...

My method of thinking (which is quite possibly wrong) is to create Array2, then loop through each element in Array1, extracting the first number. From there, check to see whether that number is in Array2 as one of the keys. If it is not, add the key, and then add all of the information about that car (the string, Element 1), but as the first element of an array of cars. If the key already exists, then just add the entire car (string) into the value of that key. Since there can be multiple cars from the same dealer, the next value would have to be added to the end of the value array.

I need to be using Array1 to accomplish this though, unfortunately.
« Last Edit: November 13, 2006, 09:08:51 PM by zymurgy »

Offline doni49

  • Devotee
  • Posts: 531
    • View Profile
Re: Associative array help
« Reply #3 on: November 13, 2006, 09:04:02 PM »
if your arrays are formated exactly as you've shown then this should do it for you:

Code: [Select]
$allCars[]="6145|Jones Jeep|Red|Truck";
$allCars[]="5542|McDaniel Chevrolet|Green|Sedan";
$allCars[]="7765|Matthews Chevrolet|Green|Van";
$allCars[]="2234|James Dodge|Blue|Truck";
$allCars[]="6612|Smith Nissan|Black|Sedan";
$carList = asort($allCars);

And if you want to pull out a certain piece of info about one of the car:
$car = explode("|", $carList[1]);
echo "Dealership Name:  " . $car[1];

This will be sorted by dealership number and for any dealerships with multiple cars listed, will be sorted within those groups based on the color then on model type.
« Last Edit: November 13, 2006, 09:06:13 PM by doni49 »
Don

Offline ToonMariner

  • Fanatic
  • Posts: 3,715
  • Gender: Male
    • View Profile
Re: Associative array help
« Reply #4 on: November 13, 2006, 09:07:38 PM »
Sorry didn't make it clear.  an assocciative array is simply an array that has strings as keys. In your case Car1, Car2 and so on - which may as well just be numeric indices.

What you haveis a one dimensional array which (if possible) is A LOT harder to work with than the array my code would create for you.
follow me on twitter @PHPsycho