stricks1984 Posted July 25, 2008 Share Posted July 25, 2008 Hi everyone. The following code: $result = mssql_query("SELECT $col, $id FROM $table;"); // Put each name retrieved from the DB into a list. while($temp = mssql_fetch_row($result)){ if($type == 'title_prefixes' || $type == 'degree_types'){ echo 'temp[0] = '.$temp[0]. ' and temp[1]: '.$temp[1].'<br />'; } array_push($list, array(trim($temp[0]), $temp[1])); if($type == 'title_prefixes' || $type == 'degree_types'){ echo '<br /> <br />List is now'.print_r($list). '<br />'; } } if($type == 'title_prefixes' || $type == 'degree_types'){echo '<br /><br />array out put: '.print_r($list);} Produces this output: ... [b]List is now1 Array ( [0] => Array ( [0] => B.S. [1] => 1 ) [1] => Array ( [0] => B.S.M.S. [1] => 2 ) [2] => Array ( [0] => M.S. [1] => 3 ) [3] => Array ( [0] => [1] => 4 ) ) // NOTICE THIS DOES NOT FLATTEN, CORRECT array out put: 1Array ( [0] => Array ( [0] => [1] => ) [1] => Array ( [0] => [1] => 4 ) [2] => Array ( [0] => B.S. [1] => 1 ) [3] => Array ( [0] => B.S.M.S. [1] => 2 ) [4] => Array ( [0] => M.S. [1] => 3 ) ) Items: 1[/b] .... ... [b]List is now1 Array ( [0] => Array ( [0] => [1] => 1 ) [1] => Array ( [0] => Dr. [1] => 2 ) [2] => Array ( [0] => Mr. [1] => 3 ) [3] => Array ( [0] => Ms. [1] => 4 ) ) // FLATTENS!!! I did not do anything, it's the exact same loop and the only call between the two echo statements is NOTHING. array out put: 1Array ( [0] => [1] => Dr. [2] => Mr. [3] => Ms. ) Items: 1 [/b] ... There is nothing different about how the arrays are built and as you can see at ListNow1 it is a 2D array in the 2nd and 1st examples but only in the 2nd does it flatten. These are both being built within the sam eloop and nothing is done in between the two echo statements...Does anyone see anything?? Link to comment https://forums.phpfreaks.com/topic/116603-solved-2d-array-is-being-flattened-into-a-1d-array-and-im-not-doing-it/ Share on other sites More sharing options...
stricks1984 Posted July 25, 2008 Author Share Posted July 25, 2008 To make it simpler: // Put each name retrieved from the DB into a list. while($temp = mssql_fetch_row($result)){ array_push($list, array(trim($temp[0]), $temp[1])); echo '<br /> <br />List is now'.print_r($list). '<br />'; } echo '<br /><br />array out put: '.print_r($list); From that code I am getting this output: List is now1 Array ( [0] => Array ( [0] => [1] => 1 ) [1] => Array ( [0] => Dr. [1] => 2 ) [2] => Array ( [0] => Mr. [1] => 3 ) [3] => Array ( [0] => Ms. [1] => 4 ) ) array out put: 1Array ( [0] => [1] => Dr. [2] => Mr. [3] => Ms. ) How is this possible? Link to comment https://forums.phpfreaks.com/topic/116603-solved-2d-array-is-being-flattened-into-a-1d-array-and-im-not-doing-it/#findComment-599570 Share on other sites More sharing options...
stricks1984 Posted July 25, 2008 Author Share Posted July 25, 2008 Ok, I fixed it. I'm not sure why, but I was pulling 4 values from the DB to populate the list and one of them was white space, which is what I think caused the error. Still a dilemma to me though. Link to comment https://forums.phpfreaks.com/topic/116603-solved-2d-array-is-being-flattened-into-a-1d-array-and-im-not-doing-it/#findComment-599588 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.