0

I'm bringing an array into jquery from php, on a wordpress website. It is a multidimensional array that looks like this after I convert into $jqueryArray (taken from console.log)

2 : Object { max=" 10", min=" 500 ", number=" 2 "}

3 : Object { max=" 15", min=" 750 ", number=" 3 "}

4 : Object { max=" 8", min=" 400 ", number=" 4 "}

5 : Object { max=" 12", min=" 700 ", number=" 5 "}

1 : Object { max=" 10", min=" 500 ", number="1 "}

The code is as follows:

 jQuery(function() { jQuery('.wpsc_select_variation').change(function(){ var arrayFromPHP = <?php echo json_encode($alt_tables) ?>; var $jqueryArray = {}; jQuery.each(arrayFromPHP, function (key, value) { $jqueryArray[key] = {}; $jqueryArray[key] = value; }); console.log($jqueryArray); // clears the div that we will type the Table Minimum order too jQuery('#table-details').empty(); var $selectedName; // returns an integer, specific to the Table # selected $selectedName = jQuery(this).find(':selected').text().replace('Table ', ''); console.log($jqueryArray.$selectedName); var $newDetails = 'Table minimum order: '; jQuery('#table-details').append( $newDetails ); }); }); 

For some reason $jqueryArray.$selectedName is undefined. I can see that $jqueryArray has 5 keys, numbered 1 through 5, but even when i try console.log($jqueryArray.1); I get undefined. I can't seem to figure out how to call the number from the array. Basically I want

$jqueryArray[$selectedName][min] I've tried $jqueryArray[$selectedName] in the console.log and receive undefined as well

I added a jsfiddle http://jsfiddle.net/kzuyd/14/

I took the <?php echo json_encode($alt_tables) ?> and just added it as the variable, since php doesn't work in jsfiddle. Hopefully it works the same..

7
  • You're not adding $selectedName to $jqueryArray... Commented Sep 20, 2012 at 0:31
  • How would I do that? $selectedName is an integer, like 1 or 2, and $jqueryArray has integers 1 through 5 as keys Commented Sep 20, 2012 at 0:48
  • I've tried console.log($jqueryArray["1"]); and console.log($jqueryArray[1]); and both return undefined Commented Sep 20, 2012 at 0:55
  • Post a jsfiddle if possible it would be helpful. Commented Sep 20, 2012 at 1:05
  • jsfiddle.net/kzuyd/14 Commented Sep 20, 2012 at 1:54

1 Answer 1

1

Updated jsFiddle

jQuery(function() { var arrayFromPHP = '{"1 ":{"note":null,"max":" 10","min":" 500 ","number":"1 "}," 2 ":{"note":null,"max":" 10","min":" 500 ","number":" 2 "}," 3 ":{"note":null,"max":" 15","min":" 750 ","number":" 3 "}," 4 ":{"note":null,"max":" 8","min":" 400 ","number":" 4 "}," 5 ":{"note":null,"max":" 12","min":" 700 ","number":" 5 "}}'; var $jqueryArray = {}; // you must parse `arrayFromPHP`, it's not actually an array now // it's a JSON string jQuery.each(JSON.parse(arrayFromPHP), function(key, value) { // your keys have spaces at the end of them e.g. "1 " // trim them first $jqueryArray['Table' + key.trim()] = {}; $jqueryArray['Table' + key.trim()] = value; }); jQuery('.wpsc_select_variation').change(function() { jQuery('#table-details').empty(); jQuery('#table-minimum').empty(); var $selectedName; $selectedName = jQuery(this) .find("option:selected") .text() .replace(/\s/g, ''); // to replace all spaces, use the regex shown var $newDetails = 'The selected name: ' + $selectedName + ''; jQuery('#table-details').append($newDetails); // use obj["min"] to get the minimum var $tableMin = '<br /><br />The table minimum is: ' + $jqueryArray[$selectedName]["min"] + ''; jQuery('#table-minimum').append($tableMin); }); });​ 
Sign up to request clarification or add additional context in comments.

3 Comments

Was the issue the fact that some variables had extra spaces hanging around? Regardless, thank you for the help!! I have spent hours on this
There were a couple of issues, mainly the ones I commented in my code above. If I had to blame one issue though it would be your .each() loop, since your 'array' variable is actually just a string, it iterates over its characters instead.
That issue was only in the fiddle. The original code was var arrayFromPHP = <?php echo json_encode($alt_tables) ?>; but I couldn't bring that into the fiddle directly. Glad you noticed it and fixed it, thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.