onlyican Posted January 24, 2007 Share Posted January 24, 2007 Hey pepsI have an array like thisArray ( [A/1] => 1-1 [A/2] => 1-1 [A/3] => 1-1 [A/4] => 1-1 [A/5] => 1-1 [A/6] => 1-1 [A/7] => 1-1 [A/8] => 1-1 [A/9] => 1-1 [A/10] => 1-1 [B/1] => 1-1 [B/2] => 1-1 [B/3] => 1-1 [B/4] => 1-1 [B/5] => 1-1 [B/6] => 1-1 [B/7] => 1-1 [B/8] => 1-1 [B/9] => 1-1 [B/10] => 1-1 [C/1] => 1-1 [C/2] => 1-1 [C/3] => 1-1 [C/4] => 1-1 [C/5] => 1-1 [C/6] => 1-1 [C/7] => 1-1 [C/8] => 1-1 [C/9] => 1-1 [C/10] => 1-1 [D/1] => 2-1 [D/2] => 1-1 [D/3] => 1-1 [D/4] => 1-1 [D/5] => 1-1 [D/6] => 1-1 [D/7] => 1-1 [D/8] => 1-1 [D/9] => 1-1 [D/10] => 1-1 [E/1] => 2-1 [E/2] => 2-1 [E/3] => 1-1 [E/4] => 1-1 [E/5] => 1-1 [E/6] => 1-1 [E/7] => 1-1 [E/8] => 1-1 [E/9] => 1-1 [E/10] => 1-1 )in phpI want to parse the keys and values to a javascript array.I know how to parse the values to a javascript array, but how to I parse the keys as well?Cheers in advance Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 24, 2007 Share Posted January 24, 2007 foreach($array AS $k=>$v){ //print js in here - i don't know the js syntax for array, post what you have.} Quote Link to comment Share on other sites More sharing options...
onlyican Posted January 24, 2007 Author Share Posted January 24, 2007 I know that bit, I dont know the Javascript Syntax eitherFor just parsing the values its something like[code]<script type='text/javascript'>var MyArray = new Array();<?phpforeach($array as $value){print "MyArray.push(\"$value\");";?>}</script>[/code]But I dont know how to parse the keys, as they are different and important Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 24, 2007 Share Posted January 24, 2007 Did you read what I posted? I showed you how to get the key of the PHP array. If you're asking how to put it in your JS array, that's a Javascript question. Maybe instead of using .push, just assign them, if you want the keys to be the same.foreach($array AS $k=>$v){ print 'MyArray['.$k.'] = '.$v;} Quote Link to comment Share on other sites More sharing options...
onlyican Posted January 24, 2007 Author Share Posted January 24, 2007 Thats the problemIn Javascript there is a different syntax, and the key of an array is not as php (well, I am getting error on that line)I know php very well, its the javascript side I am having trouble in//Added ThisI tried the following[code]<script type="text/javascript">var TopLeft = new Array();<?phpforeach ($TopLeftArray as $key => $value){ print "TopLeft.push([$key] \"$value\");";}?></script><script type="text/javascript">var TopLeft = new Array();<?phpforeach ($TopLeftArray as $key => $value){ print "TopLeft[".$key."] => $value);}?></script>And other options[/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 24, 2007 Share Posted January 24, 2007 I just got that syntax from the javascript docs! I know you can define arrays that way as I just tried it on some js code and it worked.This is a javascript problem and belongs in that forum. Quote Link to comment Share on other sites More sharing options...
onlyican Posted January 24, 2007 Author Share Posted January 24, 2007 There is a javascript forum now???How do we move it?Mods?and delete the crappy post pleaseCheers guys Quote Link to comment Share on other sites More sharing options...
paul2463 Posted January 24, 2007 Share Posted January 24, 2007 [code]<?phpfunction arrayToJS($array){$str = "";foreach($array as $val): $str .= "\"".$val."\",";endforeach;$str = rtrim($str, ",");echo $str;}[/code]with a javascript function[code]function getJSarray(){var jsarray = new Array(<?=arrayToJS($phpArray) ?>);return(jsarray);}[/code]then just call getJSarray() from somewhere in your <script> </script> tags Quote Link to comment Share on other sites More sharing options...
onlyican Posted January 24, 2007 Author Share Posted January 24, 2007 cheers, but I need to key from the PHP array as well, this is the bit I am having trouble with Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 24, 2007 Share Posted January 24, 2007 Fix the error in the code you posted and it will work.<?phpforeach ($TopLeftArray as $key => $value){ print "TopLeft[".$key."] = $value";}?> Quote Link to comment Share on other sites More sharing options...
onlyican Posted January 24, 2007 Author Share Posted January 24, 2007 ok I just found thatI thought I tried that earlier and got an error, anywayCheers 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.