943,603 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3383
  • PHP RSS
Sep 24th, 2009
0

php array to javascript array in joomla

Expand Post »
please help me to convert php multi dimension array to JavaScript multi dimension array
PHP Syntax (Toggle Plain Text)
  1. Array ( [nonveg] => Array ( [0] => mutton [1] => Chicken ) [veg] => Array ( [2] => sambar [3] => rasam ) )
this is my array value i pass it to JavaScript function i want it to retrieve their in multi dimension array is any body help me how to do that
Last edited by Ezzaral; Sep 24th, 2009 at 12:33 pm. Reason: Fixed this mess of a post.
Similar Threads
Reputation Points: 0
Solved Threads: 1
Light Poster
dorkwad is offline Offline
34 posts
since Jun 2009
Sep 24th, 2009
0

Re: php array to javascript array in joomla

Try the code below. You will have to decide what you will do with the js multi dimensional array in the javascript code where there is alert('...').

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. //create your multi lists here
  4. $list1 = array("php", "asp.net", "javascript");
  5. $list2 = array("excellent", "good", "brilliant++");
  6. $list3 = array("that", "was", "easy !");
  7.  
  8. //pass your multi lists to this key variable, then browse this file
  9. $multiList = array($list1, $list2, $list3);
  10.  
  11. //don't edit if not sure below this point
  12. $jsMultiList = "";
  13. $jsArray = array();
  14. $i = 0;
  15. foreach($multiList as $array){
  16. $commaString = "";
  17. foreach($array as $item => $value){
  18. $commaString .= '"'.$value.'",' ; //building js string with comma separators e.g: "php","asp",
  19. }
  20. $commaString = rtrim($commaString, ","); // removing the trailing comma
  21. $jsArray[$i] = "[$commaString]" ; // pass the comma separated string to an array
  22. $i++;
  23. }
  24.  
  25. $i = 0;
  26. $count = count($jsArray);
  27. foreach($jsArray as $item => $string){
  28. $arrayItem = $jsArray[$i];
  29. $jsMultiList .= "multiList[$i] = $arrayItem; \n"; //initialising the js array, item by item
  30. $i++;
  31. }
  32.  
  33. //outputting the javascript array
  34. echo "<script language='javascript'>
  35. var multiList = Array($count);
  36. $jsMultiList
  37. for(var i=0; i<multiList.length; i++)
  38. for(var j=0; j<multiList[i].length ; j++)
  39. alert(multiList[i][j]);
  40. </script>";
  41.  
  42. ?>
Last edited by wilch; Sep 24th, 2009 at 11:41 am.
Reputation Points: 21
Solved Threads: 15
Junior Poster in Training
wilch is offline Offline
76 posts
since Aug 2007
Sep 24th, 2009
0

Re: php array to javascript array in joomla

Or you could just do
php Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. var somejsarray = <?php echo json_encode($somephparray) ?>;
  3. </script>
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Sep 24th, 2009
0

Re: php array to javascript array in joomla

Wow - now that's real efficiency.
I should have known of this months ago.
Reputation Points: 21
Solved Threads: 15
Junior Poster in Training
wilch is offline Offline
76 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Ethical Issues Help
Next Thread in PHP Forum Timeline: display field value using php checkbox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC