Jump to content

PHP array 2 Javascript


onlyican

Recommended Posts

Hey peps

I have an array like this

Array ( [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 php

I 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
Link to comment
Share on other sites

I know that bit, I dont know the Javascript Syntax either

For just parsing the values its something like
[code]
<script type='text/javascript'>
var MyArray = new Array();
<?php

foreach($array as $value){
print "MyArray.push(\"$value\");";
?>
}
</script>
[/code]

But I dont know how to parse the keys, as they are different and important
Link to comment
Share on other sites

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;
}
Link to comment
Share on other sites

Thats the problem

In 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 This

I tried the following

[code]
<script type="text/javascript">
var TopLeft = new Array();

<?php
foreach ($TopLeftArray as $key => $value){
print "TopLeft.push([$key] \"$value\");";
}
?>
</script>

<script type="text/javascript">
var TopLeft = new Array();

<?php
foreach ($TopLeftArray as $key => $value){
print "TopLeft[".$key."] => $value);
}
?>
</script>

And other options
[/code]
Link to comment
Share on other sites

[code]
<?php
function 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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.