Apologies if I'm doing something very obviously stupid here, I'm fairly new to this sort of thing.

What I'm basically trying to do is crop a two dimensional array of coordinates to the same length as another array called from an sql database using php and then made useable in javascript via json_encode. The good news is that I've managed to accomplish this. The code below actually does this twice with the database information called in different orders so that I have two two-dimensional javascript arrays, each with an x and y coodinate tagged onto the end of each row.

(For anyone who's interested in the whys and wherefores, the database contains information abpout different image galleries (title, description, thumbnail etc). I'm going to be using these two arrays along with jquery animate to allow visitors to the site to change the order in which thumbnails are displayed based on date gallery updated or gallery size).

This all sort of works fine. There are a couple of functions, 'prune' which is the parent function (I think I'm using the terminology correctly) and two similar but separate 'populateArray' functions (1 and 2).

The trouble is that the two inner functions don't seem to want to return a value. I can access the two arrays from within the populateArray functions but as soon as I try to return the values (as 'galDate' and 'galSize') I get nothing.

Code is laid out below.

Like I say, apologies if I'm making a really basic mistake here but I'm completely stumped and could do with a nod in the right direction.

Thanks in advance.

var sqlData1 = (<?php print json_encode($sqlData1);?>);
var sqlData2 = (<?php print json_encode($sqlData2);?>);

var coordinates = [

//Function to make arrays the same length    
function pruneArrays(sqlData1,sqlData2, coordinates){
    for(i=(coordinates.length-sqlData1.length); i>0; i--){
  populateArray1(sqlData1, coordinates);
  populateArray2(sqlData2, coordinates);
//Function to create date ordered array with coordinates
function populateArray1 (sqlData1, coordinates){
  for(i=0; i<sqlData1.length; i++){
      for(j=0; j<(sqlData1.length-1); j++){
  var galDate = sqlData1;  
  //document.write(galDate); - if this line is active 'galDate' displays no problem
  //document.write ("<br /><br />");
  return (galDate);

//Function to create size ordered array with coordinates
function populateArray2 (sqlData2, coordinates){
  for(i=0; i<sqlData2.length; i++){
      for(j=0; j<(sqlData2.length-1); j++){
  var galSize = sqlData2;
  //document.write(galSize);  - if this line is active 'galSize' displays no problem
  return (galSize);
document.write(pruneArrays(sqlData1, sqlData2, coordinates));
// The line above however, doesn't return anything.