stijn0713 Posted June 20, 2012 Share Posted June 20, 2012 what's the best way/ which functions to use to easily add rows and colums to an existing array (matrix)? th in adv! Quote Link to comment https://forums.phpfreaks.com/topic/264502-adding-rows-and-colums-to-a-two-dimentional-array/ Share on other sites More sharing options...
PravinS Posted June 20, 2012 Share Posted June 20, 2012 try using array_push() Quote Link to comment https://forums.phpfreaks.com/topic/264502-adding-rows-and-colums-to-a-two-dimentional-array/#findComment-1355473 Share on other sites More sharing options...
stijn0713 Posted June 20, 2012 Author Share Posted June 20, 2012 mmm that doesnt really help Quote Link to comment https://forums.phpfreaks.com/topic/264502-adding-rows-and-colums-to-a-two-dimentional-array/#findComment-1355479 Share on other sites More sharing options...
ManiacDan Posted June 20, 2012 Share Posted June 20, 2012 So then...explain better. There are dozens of ways to add things to an array. The three most common: $arr[] = $something; array_push($arr, $something); array_unshift($arr, $something); If those don't fit your requirements, refine the requirements. Quote Link to comment https://forums.phpfreaks.com/topic/264502-adding-rows-and-colums-to-a-two-dimentional-array/#findComment-1355483 Share on other sites More sharing options...
stijn0713 Posted June 20, 2012 Author Share Posted June 20, 2012 Ok. This is my two dimentional array: Array ( [0] => Array ( [0] => 21 [1] => 24 [2] => 18 [3] => 1 [4] => 0 [5] => 0 [6] => 0 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 0 [4] => 1 [5] => 0 [6] => 0 ) [2] => Array ( [0] => 24 [1] => 29 [2] => 32 [3] => 0 [4] => 0 [5] => 1 [6] => 0 ) [3] => Array ( [0] => -1 [1] => -1 [2] => 2 [3] => 0 [4] => 0 [5] => 0 [6] => 1 ) ) which corresponds to this matrix: 21 24 18 1 0 0 0 01 02 03 0 1 0 0 24 29 32 0 0 1 0 -1 -1 02 0 0 0 1 Which is indexed as: 00 01 02 03 04 05 06 10 11 .... 20 21 ... etc. Now i need to add this array (x1 => 7500, x2 => 8200, x3 => 10500, t1 => 0, t2 =>0, t3 => 0, t4 => 0) so that my matrix will be: 7500 8200 10500 0 0 0 0 x1 x2 x3 t1 t2 t3 t4 21 24 18 1 0 0 0 1 2 3 0 1 0 0 24 29 32 0 0 1 0 -1 -1 2 0 0 0 1 So in fact i need to add the values or the keys of an array into a row of a two dimentional array... Hope it clears things out Quote Link to comment https://forums.phpfreaks.com/topic/264502-adding-rows-and-colums-to-a-two-dimentional-array/#findComment-1355492 Share on other sites More sharing options...
Jessica Posted June 20, 2012 Share Posted June 20, 2012 That made me dizzy. Quote Link to comment https://forums.phpfreaks.com/topic/264502-adding-rows-and-colums-to-a-two-dimentional-array/#findComment-1355495 Share on other sites More sharing options...
Mahngiel Posted June 20, 2012 Share Posted June 20, 2012 Foremost, Arrays do not have rows and columns. They have keys and values. Array ( [0] => Array ( [0] => 21 [1] => 24 [2] => 18 3] => 1 [4] => 0 5] => 0 [6] => 0 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3[3] => 0 [4] => 1[5] => 0 [6] => 0 ) [2] => Array ( [0] => 24 [1] => 29 [2] => 32 [3] => 0 [4] => 0 [5] => 1 [6] => 0 ) [3] => Array ( [0] => -1 [1] => -1 [2] => 2 [3] => 0 [4] => 0 [5] => 0 [6] => 1 ) ) Now i need to add this array (x1 => 7500, x2 => 8200, x3 => 10500, t1 => 0, t2 =>0, t3 => 0, t4 => 0) so that my matrix will be: 7500 8200 10500 0 0 0 0 x1 x2 x3 t1 t2 t3 t4 Are the x#'s supposed to be keys or values? You show them both ways. Quote Link to comment https://forums.phpfreaks.com/topic/264502-adding-rows-and-colums-to-a-two-dimentional-array/#findComment-1355512 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.