Jump to content

[SOLVED] 2D Array is Being Flattened into a 1D array and I'm not doing it...


stricks1984

Recommended Posts

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??

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?

Archived

This topic is now archived and is closed to further replies.

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