Sunny Kumar
Sunny Kumar

Reputation: 1

PHP: Order of array keys is not coming up correctly

I have an array (large) with with 120 rows and each row having 5 columns. At the end I tried to add three more rows but only the last three columns. First I added dummy char * to the rows (3) I wanted to append. In a loop, these three rows (each three columns) are added. Problem is - the first row that is added at the end of main row, the order of keys is lost!! Example of Output (showing only the last part):

[116] => Array ( [0] => A3 [1] => B4 [2] => C2 [3] => D4 [4] => * ) 
[117] => Array ( [0] => A3 [1] => B5 [2] => C2 [3] => D2 [4] => * ) 
[118] => Array ( [0] => A3 [1] => B5 [2] => C2 [3] => D3 [4] => * ) 
[119] => Array ( [0] => A3 [1] => B5 [2] => C2 [3] => D4 [4] => * ) 
[120] => Array ( **[4]** => * [0] => * [1] => * [2] => * [3] => * ) **<==== Observer this row**
[121] => Array ( [0] => * [1] => * [2] => * [3] => * [4] => * ) 
[122] => Array ( [0] => * [1] => * [2] => * [3] => * [4] => * )

If you see row 120, [4] came in the start, instead of [0]!

Code Used to populate dummy * :

$i = 0;
for ($i=0;$i<=count($unMatchedRows)-1;$i++){
    for ($jCols = 0; $jCols<=($pNoOfCols+$pStartCol-1); $jCols++){
        $pMainArray[$vRowsOfMainArray+$i][$jCols] = '*';
    }
}

Code used to update the last three rows:

$i=0;
$ColToStart = $pStartCol-1;
foreach ($unMatchedRows as $rowNumberOfCompArray) {
    for ($jCols = 0; $jCols<=$pNoOfCols; $jCols++){
        echo "<br /> Value of Colstart: ".($ColToStart+$jCols);
        $pMainArray[$vRowsOfMainArray+$i][$ColToStart+$jCols] =       $pCompArray[$rowNumberOfCompArray][$jCols];

    }
    $i++;
}
$pStartCol = 3 and $pNoOfCols = 2;

Did a echo of row and col values and things are shown up perfect!! Even on retriving, i could get the right values ... like for [0] and [4] I was getting right values.

Problem is need to feedback the above array in the visual environment and it is not showing up correctly.

Any help?

Upvotes: 0

Views: 154

Answers (1)

Savageman
Savageman

Reputation: 9487

I'm not sure about your problem, but the keys will appear in the same order they are appended to the array.

You can use ksort() to sort an array based on the keys.

Upvotes: 1

Related Questions