zymurgy Posted November 14, 2006 Share Posted November 14, 2006 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 etcThere are a variable number of different unique dealer numbers, but many of the cars belong to the same dealers. ExampleCar 1: 6145, Foo Auto, Red, Sedan, etc etc etcCar 2: 4538, Bar Auto, Blue, Truck, etc etc etcCar 3: 6145, Foo Auto, Black, Van, etc etc etcCar 4: 5840, Hello World, White, Sedan, etc etc etcCar 5: 4538, Bar Auto, Yellow, Submarine, etc etc etcThose 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. Quote Link to comment https://forums.phpfreaks.com/topic/27170-associative-array-help/ Share on other sites More sharing options...
ToonMariner Posted November 14, 2006 Share Posted November 14, 2006 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... Quote Link to comment https://forums.phpfreaks.com/topic/27170-associative-array-help/#findComment-124241 Share on other sites More sharing options...
zymurgy Posted November 14, 2006 Author Share Posted November 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/27170-associative-array-help/#findComment-124242 Share on other sites More sharing options...
doni49 Posted November 14, 2006 Share Posted November 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/27170-associative-array-help/#findComment-124244 Share on other sites More sharing options...
ToonMariner Posted November 14, 2006 Share Posted November 14, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/27170-associative-array-help/#findComment-124246 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.