/    Sign up×
Community /Pin to ProfileBookmark

trouble declaring parameter as an array

Hello people, I have a php script which calls a javascript function with a parameter. I am having trouble getting this parameter act as an array inside the javascript function:

php:

[CODE]
<? $array1 = ‘”apple”,”orange”,”banana”‘;
$array2 = ‘”corn”,”bean”,”beet”‘;
$arrays = $array1.’///’.$array2;

<script type=”text/javascript”>
jsfunc(‘<? echo $arrays;?>’);
</script>
[/CODE]

js:

[CODE]
function jsfunc(arrays) {
array = arrays.split(“///”);

fruits = Array(array[0]);
vegs = Array(array[1]);

alert(fruits[0]); //outputs all the elements, instead of just “apple”

}
[/CODE]

JS sees fruits and vegs as a string rather then an array, so if I try to output fruits[0]; it gives me the entire string rather then seeing it as an array and giving me the first element.

What am I doing wrong?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineJun 11.2010 — [CODE][FONT="Courier New"]function jsfunc(arrays) {
array = arrays.split("///"); [B]// creates an array of 2 strings[/B]

fruits = array[0].split( ',' ); // creates array from string
vegs = array[1].split( ',' ); // " " " " "

alert(fruits[0]); //outputs just "apple"

}[/FONT][/CODE]
Copy linkTweet thisAlerts:
@SparoHawkJun 11.2010 — Why don't you just print out a JS array?

Structure is simple:
[CODE]<?php
$array1 = array('"apple"', '"orange"', '"banana"');
$array2 = array('"corn"', '"bean"', '"beet"');

echo 'var jsArray = [];',"n",
' jsArray["fruit"] = [', join(',', $array1),'];',"n",
' jsArray["veg"] = [', join(',', $array2),'];';
?>[/CODE]


Though I like the object way better:
[CODE]<?php
$array1 = array('"apple"', '"orange"', '"banana"');
$array2 = array('"corn"', '"bean"', '"beet"');

$array1 = join(',', $array1);
$array2 = join(',', $array2);

echo <<<JS
var jsArray = {
fruits: [{$array1}],
vegs: [{$array2}]
}
JS;
?>[/CODE]
Copy linkTweet thisAlerts:
@yahooman123authorJun 11.2010 — [CODE][FONT="Courier New"]function jsfunc(arrays) {
array = arrays.split("///"); [B]// creates an array of 2 strings[/B]

fruits = array[0].split( ',' ); // creates array from string
vegs = array[1].split( ',' ); // " " " " "

alert(fruits[0]); //outputs just "apple"

}[/FONT][/CODE]
[/QUOTE]


Hey, this created an array, however it is outputting the word with the quotes around it. How can I remove the quotes from the output?

I have tried using:

[CODE]array[0].replace( '/"/g', '' ).split( ',' );[/CODE]

but it no longer becomes an array?
Copy linkTweet thisAlerts:
@yahooman123authorJun 11.2010 — Nevermind, I just took out the quote from the php variables. Thanks for you help guys!
×

Success!

Help @yahooman123 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...