cmor Posted July 21, 2007 Share Posted July 21, 2007 Very simple question but I'm having trouble finding an answer by searching the forum. I have a 2-d array. I want to pass columns from the 2-d array to a file that will graph the columns. Let's say its called my2darray I can pass a row with something like my2darray[4] But I can't pass a specific column with my2darray[][4] Any help with the syntax or a link to the solution would be helpful. Do I have to use a loop to call a whole column? Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 21, 2007 Share Posted July 21, 2007 try <?php function cols2rows(&$array) { $newarray = array(); foreach ($array as $ar) { $k = count($ar); for ($i=0; $i<$k; $i++) { $newarray[$i][] = $ar[$i]; } } return $newarray; } $my2darray = array ( array ('a', 1,2,3,4), array ('b', 2,3,4,5), array ('c', 3,4,5,6) ); $new2d = cols2rows($my2darray); ?> Now pass $new2d[4] to the graph. Quote Link to comment Share on other sites More sharing options...
cmor Posted July 21, 2007 Author Share Posted July 21, 2007 Thanks, so to confirm there's no way to call a column? You have to switch the array's dimensions around? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 21, 2007 Share Posted July 21, 2007 That, or loop through the array each time gathering the contents of the Nth element. $data = array(); foreach ($my2darray as $ar) { $data[] = $ar[4]; } As you said you wanted to pass columns (plural) to the graph, I thought you may as well do them all in one operation Quote Link to comment 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.