
pineapple1
Members-
Posts
25 -
Joined
-
Last visited
Never
Profile Information
-
Gender
Not Telling
pineapple1's Achievements

Newbie (1/5)
0
Reputation
-
Anybody have any thoughts? What's wrong with my array.....? The code works fine when using Crayon Violet's array.
-
Still stuck Any suggestions?
-
Thank you for working through this with me. My PHP skills need a lot of work... but I'm getting there. I've taken everything you've said into consideration and I think I should have something that works.. but it doesn't The code still works great when I use your array. So I know it must be something with mine. But I can't figure out what?!?! Here is the code as it stands today: <?php //Open and read CSV file example has four columns $i=-1; $file=fopen('testdata.csv','r') or die("Can't open file"); while(!feof($file)){ $i++; $data[$i]=fgetcsv($file,1024); $data[$i]= array('serial'=>$data[$i][0], 'type'=>$data[$i][1], 'width1'=>$data[$i][2], 'height1'=>$data[$i][3]); } //Make columns sortable for each sortable column foreach ($data as $key => $row) { $serial[$key] = $row[0]; $type[$key] = $row[1]; $width1[$key] = $row[2]; $height1[$key] = $row[3]; } array_multisort($type, $width1, $height1, $data); $prev = array(); $current = 0; foreach ($data as $row) { $diff = array_diff($row,$prev); unset($diff['serial']); if (count($diff) == 0) { $newList[$current]['serial'] .= ',' . $row['serial']; } else { $current++; $newList[$current] = $row; } $prev = $row; } echo "<pre>"; print_r($newList); ?> The output looks like this: As you can see, keys 4 and 6 should be grouped together, but they're not. I've also noticed that my multi_sort is not working right. I think this is probably the key to the problem. I must be doing this wrong
-
Sorry, I was messing around with it trying to get it to work. No luck. I take it I have to have the while loop like I originally had it right? Does your code go inside that loop then? Like it is here: <?php $i=-1; $file=fopen('glass.csv','r') or die("Can't open file"); while(!feof($file)){ $i++; $array[$i]=fgetcsv($file,1024); //CRAYON VIOLET CODE. $prev = array(); $current = 0; // $array is your current multidim array foreach ($array as $row) { $diff = array_diff($row,$prev); unset($diff['serial']); if (count($diff) == 0) { $newList[$current]['serial'] .= ',' . $row['serial']; } else { $current++; $newList[$current] = $row; } $prev = $row; } echo "<pre>"; print_r($newList); } ?>
-
Nah... Here's the full code - I've been messing around with it so it might look a little different now: <?php //Open and read CSV file $file=fopen('glass.csv','r') or die("Can't open file"); $data=fgetcsv($file,1024); $prev = array(); $current = 0; foreach ($data as $row) { $diff = array_diff($row,$prev); unset($diff['serial']); if (count($diff) == 0) { $newList[$current]['serial'] .= ',' . $row['serial']; } else { $current++; $newList[$current] = $row; } $prev = $row; } echo "<pre>"; print_r($newList); ?> I get 3 warnings, all in reference to the array_diff. Then my output looks like this: Array ( [0] => Array ( [serial] => ,2,7,1 ) ) I'm wondering if maybe it is because some of the fields of the CSV are blank. There is still a placeholder for them, but they are blank. For example... 16,red,43mm,,45kg.
-
Wow. Works fine using your array. I did change the variable to suit my array. Perhaps it has to do with how I'm reading in the data. I'm using fgetcsv to read in a csv file. $i=-1; $file=fopen('glass.csv','r') or die("Can't open file"); while(!feof($file)){ $i++; $data[$i]=fgetcsv($file,1024); }
-
Hmmm.. that's giving me an error "argument 1 is not an array" for $diff = array_diff($row,$prev);
-
Thanks for the code. What you are suggesting for the QTY would work. However, without anyway of isolating the groups, wouldn't this function just give me the number of rows of data in the entire table? In my example, I only show a few rows but we're talking about a couple hundred sizes - maybe 30 different common ones. I think the trick to all this is determining how to group them. That's where I'm lost.
-
In order to be same group, they'd have to be the exact same piece of glass. So all columns would have to match - except the serial #
-
Well, this isn't for an inventory program or anything. Actually, you could replace Serial # with Label # or Name. They are for stickers that will go on a whole bunch of glass pieces for installation in a building. That "serial" corresponds to a specific location on the facade of the building. But I want to be able to tell the glass manufacturer what the common sizes are to make it easier to fabricate.
-
My situation is this.. I've got a multidimensional array where I'd like to group common rows and output them as a single row with a qty. Even if that quantity is just 1. Currently, my code groups the rows together by using multisort, but I have no idea how to consolidate them into a single row and qty. For example: Serial # Color Width Height Weight 16 Blue 15mm 25mm 50kg 23 Blue 15mm 25mm 50kg 42 Blue 15mm 25mm 50kg Would become Qty Serial #'s Color Width Height Weight 3 16,23,42 Blue 15mm 25mm 50kg Does that make sense? I just have no clue how best to tackle this. Right now my data is structured as a multidimensional array... $data[row][col]. Any advice would be very much appreciated!
-
My thought would be to somehow compare each line of the array containing the matches against my original array. This is my first experience with multidimensional arrays so I'm really struggling here.
-
Any thoughts? This code is working beautifully except this one issue.
-
This makes sense. However, may source data has gotten a little more complicated.... I'm kicking myself now, but I sort of "dumbed down" my problem. What I'm actually doing is trying to consolidate a list of insulated glass sizes. Insulated glass has 2 panes, not necessarily the same size, so I have 2 widths and 2 heights. This makes things messy, and it seems like the first solution would be best for this since it find exact matches. Using the code posted by PrintF, I just need a simple way to figure out which entries aren't duplicates. Sorry for not coming clean earlier
-
Should have been more specific. So sort by weight, then by height, that should order all the common entries together. I would still need a way to consolidate the common entries though.