albertdumb Posted July 9, 2007 Share Posted July 9, 2007 Hi All. I have this array like so.... Array ( [ABZ] => Array ( [0] => AGP ) [ACE] => Array ( [0] => BHX [1] => LGW [2] => LTN [3] => MAN ) ) Now i want to loop through this array to print out a javascript array in the format like >> g_routes['ACE'] = new Array('BHX','LGW','LTN','MAN'); I dont mind just using an echo or assign it a php variable. Can anyone please help? Cheers Quote Link to comment Share on other sites More sharing options...
Lumio Posted July 9, 2007 Share Posted July 9, 2007 Use foreach and str_replace Quote Link to comment Share on other sites More sharing options...
albertdumb Posted July 9, 2007 Author Share Posted July 9, 2007 I am using a foreach and i got most it done except the last bit of the array: This is my code>> foreach ($listSort as $value => $var){ print 'g_routes[\''.$value.'\']= new Array(\''.$var[0] .'\',\'LGW\');'; } I am having problem with the new array as i need to loop trough to get the next level down but i am not sure how to do it. So this is what i have now: g_routes['ABZ']= new Array(' STILL NEED TO GET','STILL NEED TO GET'); Any help? Quote Link to comment Share on other sites More sharing options...
Lumio Posted July 9, 2007 Share Posted July 9, 2007 I made something for you: <?php function makeJSArray($name, $array, $parent='') { if (empty($parent)) $r = "var $name = new Array();\n"; else $r = ''; foreach ($array as $i => $s) { if (!is_numeric($i)) $i = "'$i'"; if (is_array($s)) { $r .= "$name$parent"."[$i] = new Array();\n"; $r .= makeJSArray($name, $s, $parent."[$i]"); }else { if (!is_numeric($s)) $s = '"'.str_replace("\n", '\\n', addslashes($s)).'"'; $r .= "$name$parent"."[$i] = $s;\n"; } } return $r; } ?> Use it like that: <?php $a = array('test', 'blubb', 'hui' => array('yeah', 'hmm', 'foo' => 'bar', array('nana'))); echo makeJSArray('myjsarray', $a); ?> Quote Link to comment Share on other sites More sharing options...
albertdumb Posted July 9, 2007 Author Share Posted July 9, 2007 Thanks Lumio.. But the problem with that code is that the araay which is created has every key value iterated like so: g_routes['ACE'][0] = "BHX"; g_routes['ACE'][1] = "LGW"; g_routes['ACE'][2] = "LTN"; g_routes['ACE'][3] = "MAN"; But i want it like: g_routes['ACE'] = new Array('BHX','LGW','LTN','MAN'); i just dont know how to loop through the last dimension of the array and list it comma seperated, if you know what i mean. Quote Link to comment Share on other sites More sharing options...
Lumio Posted July 9, 2007 Share Posted July 9, 2007 Why can't you use it like my function? Quote Link to comment Share on other sites More sharing options...
albertdumb Posted July 9, 2007 Author Share Posted July 9, 2007 I have worked it out and it wasnt as complicated as i thought. Found the implode function. Heres the code i use foreach ($this->listSort as $value => $var){ $spiltVar= implode("','",$var); print 'g_routes[\''.$value.'\']= new Array(\''.$spiltVar .'\');'; } Thanks very much for your help tho. 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.