Jump to content

Associative array help


zymurgy

Recommended Posts

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]$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
[/code]

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.
Link to comment
Share on other sites

bar delimited is not an array its a string.

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

[code]<?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;
}
}
?>
[/code]

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...
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

if your arrays are formated exactly as you've shown then this should do it for you:

[code]
$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);
[/code]

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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