| | |
php array to javascript array in joomla
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2009
Posts: 27
Reputation:
Solved Threads: 0
please help me to convert php multi dimension array to JavaScript multi dimension array 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
PHP Syntax (Toggle Plain Text)
Array ( [nonveg] => Array ( [0] => mutton [1] => Chicken ) [veg] => Array ( [2] => sambar [3] => rasam ) )
Last edited by Ezzaral; Sep 24th, 2009 at 12:33 pm. Reason: Fixed this mess of a post.
•
•
Join Date: Aug 2007
Posts: 55
Reputation:
Solved Threads: 9
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)
<?php //create your multi lists here $list1 = array("php", "asp.net", "javascript"); $list2 = array("excellent", "good", "brilliant++"); $list3 = array("that", "was", "easy !"); //pass your multi lists to this key variable, then browse this file $multiList = array($list1, $list2, $list3); //don't edit if not sure below this point $jsMultiList = ""; $jsArray = array(); $i = 0; foreach($multiList as $array){ $commaString = ""; foreach($array as $item => $value){ $commaString .= '"'.$value.'",' ; //building js string with comma separators e.g: "php","asp", } $commaString = rtrim($commaString, ","); // removing the trailing comma $jsArray[$i] = "[$commaString]" ; // pass the comma separated string to an array $i++; } $i = 0; $count = count($jsArray); foreach($jsArray as $item => $string){ $arrayItem = $jsArray[$i]; $jsMultiList .= "multiList[$i] = $arrayItem; \n"; //initialising the js array, item by item $i++; } //outputting the javascript array echo "<script language='javascript'> var multiList = Array($count); $jsMultiList for(var i=0; i<multiList.length; i++) for(var j=0; j<multiList[i].length ; j++) alert(multiList[i][j]); </script>"; ?>
Last edited by wilch; Sep 24th, 2009 at 11:41 am.
Or you could just do
php Syntax (Toggle Plain Text)
<script type="text/javascript"> var somejsarray = <?php echo json_encode($somephparray) ?>; </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.
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.
![]() |
Similar Threads
- HOW to fetch php array value to javascript array (PHP)
- PHP array to javascript array (JavaScript / DHTML / AJAX)
- How use php array with javascript array (PHP)
Other Threads in the PHP Forum
- Previous Thread: PHP Ethical Issues Help
- Next Thread: display field value using php checkbox
Views: 1125 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for code, joomla
6 7 10 2002 2007 access activation apache api applet binary binarytree blogger blogging browser bug c# code codebox combo compiler convert copy creat csv daniweb data debugging design designer developer development dreamweaver dropdownlist editcodesnippet error excel exception file firewall format frontpage game gdata glitch google gpl host html http images java joomla key linus linux microsoft missing module mosaic multimedia net newbie news openbsd operatingsystems password php preview problem product program programming project prompt python reuse rss script serial snippet source strip tagging template tree tutorial vb vb.net vb2008 virtual virus visual visualbasic6 web webdeveloper website websitedeveloper windows xml







