php array to javascript array in joomla

Reply

Join Date: Jun 2009
Posts: 27
Reputation: dorkwad has a little shameless behaviour in the past 
Solved Threads: 0
dorkwad dorkwad is offline Offline
Banned

php array to javascript array in joomla

 
0
  #1
Sep 24th, 2009
please help me to convert php multi dimension array to JavaScript multi dimension array
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: php array to javascript array in joomla

 
0
  #2
Sep 24th, 2009
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('...').

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: php array to javascript array in joomla

 
0
  #3
Sep 24th, 2009
Or you could just do
  1. <script type="text/javascript">
  2. var somejsarray = <?php echo json_encode($somephparray) ?>;
  3. </script>
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: php array to javascript array in joomla

 
0
  #4
Sep 24th, 2009
Wow - now that's real efficiency.
I should have known of this months ago.
Reply With Quote Quick reply to this message  
Reply

Tags
code, joomla

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC