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 Link to comment https://forums.phpfreaks.com/topic/59063-solved-sorting-an-array-and-print-out-a-js-array/ Share on other sites More sharing options...
Lumio Posted July 9, 2007 Share Posted July 9, 2007 Use foreach and str_replace Link to comment https://forums.phpfreaks.com/topic/59063-solved-sorting-an-array-and-print-out-a-js-array/#findComment-293196 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? Link to comment https://forums.phpfreaks.com/topic/59063-solved-sorting-an-array-and-print-out-a-js-array/#findComment-293200 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); ?> Link to comment https://forums.phpfreaks.com/topic/59063-solved-sorting-an-array-and-print-out-a-js-array/#findComment-293242 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. Link to comment https://forums.phpfreaks.com/topic/59063-solved-sorting-an-array-and-print-out-a-js-array/#findComment-293319 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? Link to comment https://forums.phpfreaks.com/topic/59063-solved-sorting-an-array-and-print-out-a-js-array/#findComment-293436 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. Link to comment https://forums.phpfreaks.com/topic/59063-solved-sorting-an-array-and-print-out-a-js-array/#findComment-293444 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.