'Sorting MYSQL returns into arrays for printing causes array to be repeated unnecessarily
I have a project where I'm transcribing data from historical sources, which is then saved into a MYSQL database, and is then displayed in an HTML page, using PHP to do all the work for arranging things for display.
The problem I'm facing right now is that the code I'm using for sorting the data into arrays for display is causing the output lines to be duplicated so that the data is displayed twice, one after another.
here is the data going into the function (which I've grabbed from a var_export[edited])
So here is the results of the var_export for $lines, which is the lines passed into the function. There are 8 lines here, which is correct (Test Ships A through H) A-C are assigned to column 1 D-F are assigned to column 2 G & H are assigned to column 3
here is the var_export for section_flags array ( 0 => (object) array( 'report_section_key' => 'dsk_61ee0a39380dd', 'report_key' => 'sdr61ee09e0cea3a', 'section_number' => '1', 'ship_name_label' => 'Shipps', 'ship_label_position' => '0', 'captain_label' => 'Captains', 'captain_column_position' => '1', 'lieutenants_label' => 'N/A', 'lieutenants_used' => '-1', 'men_used' => '2', 'men_label' => 'Crew', 'guns_used' => '-1', 'guns_label' => 'N/A', 'rate_used' => '-1', 'rate_label' => 'N/A', 'location_0_used' => '-1', 'location_0_label' => 'N/A', 'location_0_field' => 'not_used', 'location_1_used' => '-1', 'location_1_label' => 'N/A', 'location_1_field' => 'not_used', 'date_cleaned_used' => '-1', 'date_cleaned_label' => 'N/A', 'date_wages_start_label' => 'N/A', 'date_wages_start_position' => '-1', 'time_in_pay_used_months' => '-1', 'time_in_pay_label_months' => 'N/A', 'how_many_columns' => '3', 'section_comment' => '', 'start_with_subtitle' => '0', 'time_in_pay_used_weeks' => '-1', 'time_in_pay_label_weeks' => 'N/A', ), )
array ( 0 => (object) array( 'ship_name_as_written' => 'Test Ship A', 'captain_name_as_written' => 'Test Captain A', 'crew' => '1', 'archival_reference' => 'Just a test Reference', 'comment' => 'None Listed', 'section_column_number' => '1', 'line_number' => '0', ), 1 => (object) array( 'ship_name_as_written' => 'Test Ship B', 'captain_name_as_written' => 'Captain B', 'crew' => '2', 'archival_reference' => 'Just a test Reference', 'comment' => 'None Listed', 'section_column_number' => '1', 'line_number' => '1', ), 2 => (object) array( 'ship_name_as_written' => 'Test Ship C', 'captain_name_as_written' => 'Test Captain C', 'crew' => '3', 'archival_reference' => 'Just a test Reference', 'comment' => 'None Listed', 'section_column_number' => '1', 'line_number' => '2', ), 3 => (object) array( 'ship_name_as_written' => 'Test Ship D', 'captain_name_as_written' => 'Test Captain D', 'crew' => '4', 'archival_reference' => 'Just a test Reference', 'comment' => 'None Listed', 'section_column_number' => '2', 'line_number' => '3', ), 4 => (object) array( 'ship_name_as_written' => 'Test Ship E', 'captain_name_as_written' => 'Test Captain E', 'crew' => '5', 'archival_reference' => 'Just a test Reference', 'comment' => 'None Listed', 'section_column_number' => '2', 'line_number' => '4', ), 5 => (object) array( 'ship_name_as_written' => 'Test Ship F', 'captain_name_as_written' => 'Test Captain F', 'crew' => '6', 'archival_reference' => 'Just a test Reference', 'comment' => 'None Listed', 'section_column_number' => '2', 'line_number' => '5', ), 6 => (object) array( 'ship_name_as_written' => 'Test Ship G', 'captain_name_as_written' => 'Test Captain G', 'crew' => '7', 'archival_reference' => 'Just a test Reference', 'comment' => 'None Listed', 'section_column_number' => '3', 'line_number' => '6', ), 7 => (object) array( 'ship_name_as_written' => 'Test Ship H', 'captain_name_as_written' => 'Test Captain H', 'crew' => '8', 'archival_reference' => 'Just a test Reference', 'comment' => 'None Listed', 'section_column_number' => '3', 'line_number' => '7', ), )
here is the var_export for Subs rray ( 0 => (object) array( 'summary_abstract_key' => 'sub_ab61ee1f5f85a31', 'report_key' => 'sdr61ee09e0cea3a', 'section_key' => 'dsk_61ee0a39380dd', 'line_number' => '8', 'field_text' => 'Third Column Group Subtitle', 'column_number' => '3', ), )
*****Here is the output of the sorting function - including the subtitles. (This actually is a var-dump of each part of the output array). I'm confused as hell because as I expected these sub arrays to have numberic keys, which are the column number (and they don't seem to), and because the last group is doubled- and I don't understand how that happens in the code.
array(3) { [3]=> array(5) { [0]=> string(11) "Test Ship D" 1=> string(14) "Test Captain D" [2]=> string(1) "4" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } [4]=> array(5) { [0]=> string(11) "Test Ship E" 1=> string(14) "Test Captain E" [2]=> string(1) "5" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } [5]=> array(5) { [0]=> string(11) "Test Ship F" 1=> string(14) "Test Captain F" [2]=> string(1) "6" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } }
array(3) { [6]=> array(5) { [0]=> string(11) "Test Ship G" 1=> string(14) "Test Captain G" [2]=> string(1) "7" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } [7]=> array(5) { [0]=> string(11) "Test Ship H" 1=> string(14) "Test Captain H" [2]=> string(1) "8" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } [8]=> array(1) { [0]=> string(27) "Third Column Group Subtitle" } }
array(3) { [6]=> array(5) { [0]=> string(11) "Test Ship G" 1=> string(14) "Test Captain G" [2]=> string(1) "7" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } [7]=> array(5) { [0]=> string(11) "Test Ship H" 1=> string(14) "Test Captain H" [2]=> string(1) "8" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } [8]=> array(1) { [0]=> string(27) "Third Column Group Subtitle" } } lines
array(3) { [3]=> array(5) { [0]=> string(11) "Test Ship A" 1=> string(14) "Test Captain A" [2]=> string(1) "1" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } [4]=> array(5) { [0]=> string(11) "Test Ship B" 1=> string(14) "Test Captain B" [2]=> string(1) "2" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } [5]=> array(5) { [0]=> string(11) "Test Ship C" 1=> string(14) "Test Captain C" [2]=> string(1) "3" [30]=> string(21) "Just a test Reference" [31]=> string(11) "None Listed" } }
as you can see, the "last" group here is duplicated- what's getting me, is I don't understand why it's only the "last" group to be duplicated- if I have group of lines, it gets duplicated, if I have two, it's only the "more recent".
here is the actual code
function newest_sort_ship_deployment_line_single_section($section_flags, $lines,$subs)
{
//var_dump ($section_flags);
//var_dump ($lines);
$section_sorted = array();
foreach ($lines as $line)
{ // use the line numeber
//echo "column number " . $line->section_column_number . "<br>";
echo "the line in question <br>"; //HAVE TO PUT THE SECTION FLAGS SHIT BACK B/C it's needed. Tomorrow
var_dump ($line);
echo "<br>";
//$section_sorted[$line->section_column_number] = array();
$section_sorted[$line->section_column_number] [$line->line_number] = array();
if ($section_flags[0]->ship_label_position != -1)
{
if ($line->ship_name_as_written == "None Listed")
{
$section_sorted[$line->section_column_number] [$line->line_number][$section_flags[0]->ship_label_position] = " "; //now, copy this
}
elseif ($line->ship_name_as_written == "N/A")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->ship_label_position] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->ship_label_position] = $line->ship_name_as_written;
}
}
if ($section_flags[0]->captain_column_position != -1)
{
if ($line->captain_name_as_written == "None Listed")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->captain_column_position] = " ";
}
elseif ($line->captain_name_as_written == "N/A")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->captain_column_position] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->captain_column_position] = $line->captain_name_as_written;
}
}
if ($section_flags[0]->lieutenants_used != -1)
{
if ($line->lieutenants_as_written == "None Listed")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->lieutenants_used] = " ";
}
elseif ($line->lieutenants_as_written == "N/A")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->lieutenants_used] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->lieutenants_used] = $line->lieutenants_as_written;
}
}
if ($section_flags[0]->men_used != -1)
{
if ($line->crew == "0")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->men_used] = " ";
}
elseif ($line->crew == "-1")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->men_used] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->men_used] = $line->crew;
}
}
if ($section_flags[0]->guns_used != -1)
{
if ($line->ship_guns == "0")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->guns_used] = " ";
}
elseif ($line->ship_guns == "-1")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->guns_used] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->guns_used] = $line->ship_guns;
}
}
if ($section_flags[0]->rate_used != -1)
{
if ($line->ship_rate == "0")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->rate_used] = " ";
}
elseif ($line->ship_rate == "-1")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->rate_used] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->rate_used] = $line->ship_rate;
}
}
$fieldtext0 = $section_flags[0]->location_0_field;
$fieldtext1 = $section_flags[0]->location_1_field;
if ($section_flags[0]->location_0_used != -1)
{ //field name is in section flags
if ($line->$fieldtext0 == "None Listed")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->location_0_used] = " ";
}
elseif ($line->$fieldtext0 == "N/A")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->location_0_used] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->location_0_used] = $line->$fieldtext0;
}
}
if ($section_flags[0]->location_1_used != -1)
{
//field name is in section flags
if ($line->$fieldtext1 == "None Listed")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->location_1_used] = " ";
}
elseif ($line->$fieldtext1 == "N/A")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->location_1_used] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->location_1_used] = $line->$fieldtext1;
}
}
if ($section_flags[0]->date_wages_start_position != -1)
{
if ($line->date_wages_start == "1000-01-01")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]-> date_wages_start_position] = " ";
}
elseif ($line->date_wages_start == "0001-01-01")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]-> date_wages_start_position] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]-> date_wages_start_position] = $line->date_wages_start;
}
}
if ($section_flags[0]->date_cleaned_used != -1)
{
if ($line->date_cleaned == "1000-01-01")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->date_cleaned_used] = " ";
}
elseif ($line->date_cleaned == "0001-01-01")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->date_cleaned_used] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->date_cleaned_used] = $line->date_cleaned;
}
}
if ($section_flags[0]->time_in_pay_used_months != -1)
{
if ($line->time_sea_pay_months == "0")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->time_in_pay_used_months] = " ";
}
elseif ($line->time_sea_pay_months == "-1")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->time_in_pay_used_months] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->time_in_pay_used_months] = $line->time_sea_pay_months;
}
}
if ($section_flags[0]->time_in_pay_used_weeks != -1)
{
if ($line->time_sea_pay_weeks == "0")
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->time_in_pay_used_weeks] = " ";
}
elseif ($line->time_sea_pay_weeks == "-1")
{
//shouldn't happen
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->time_in_pay_used_weeks] = "Not Used- Error";
}
else
{
$section_sorted [$line->section_column_number] [$line->line_number][$section_flags[0]->time_in_pay_used_weeks] = $line-> time_sea_pay_weeks;
}
}
$thing_to_sort = $section_sorted [$line->section_column_number][$line->line_number];
ksort ($thing_to_sort, SORT_NUMERIC);
$section_sorted [$line->section_column_number][$line->line_number] = $thing_to_sort;
$section_sorted [$line->section_column_number][$line->line_number][30] = $line->archival_reference;
if ($line->line_comment == NULL)
{
$section_sorted [$line->section_column_number][$line->line_number][31] = "None Listed";
}
else
{
$section_sorted [$line->section_column_number][$line->line_number][31] = $line->line_comment;
}
}
//now, add the subtitles
foreach ($subs as $sub)
{
$section_sorted [$sub->column_number] [$sub->line_number][0] = $sub->field_text;
}
/*echo "Section sorted<br>";
var_dump ($section_sorted);
echo "SS over <br>";*/
$thing_to_sort = array();
$thing_counter = 0;
//NEED TO SORT OUT THE SORTING TO GET SUBTITLES IN THE RIGHT PLACE
foreach ($section_sorted as $column)
{
$thing_to_sort = $column;
ksort ($thing_to_sort, SORT_NUMERIC);
$section_sorted[$thing_counter] = $thing_to_sort;
$thing_counter++;
}
//sort columns by line number to get them right
/*echo "section sorted <br>";
var_dump ($section_sorted);
echo "<br>";*/
return $section_sorted;
}
here a visual example of how the data is output (after being passed through another function that does the layout). Clearly, there's another issue here because the actual columns aren't in order either (I need another sort to get them in order), but you can see that G and H are repeated on the right, when they should not be.
Anyways, I'm completely at a loss to understand how more arrays are being created than column-groups and if anybody can help me spot the issue, I would very much appreciate it.
New Twist: I went and changed the "column_number" values for the lines above from 1-3 to 0-2 and things started displaying normally/perfect. So there's something in my code that objects to the array of data (that gets fed to the display function) starting with element 1 instead of element 0, and the largest number element is also used as element 0. That is really weird.
So while I've "solved" the problem in that I now have things displaying properly, I still need to solve why the code I've written does that so I can stop that behaviour.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

