Monday, 15 March 2010

Create a two dimensional javascript array from PHP array -



Create a two dimensional javascript array from PHP array -

how create php array:

array(12) { [0]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(1) "8" ["name"]=> string(10) "accounting" ["main_category_id"]=> string(1) "1" } } [1]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(1) "1" ["name"]=> string(17) "applications" ["main_category_id"]=> string(1) "2" } } [2]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(1) "2" ["name"]=> string(19) "benefit claims" ["main_category_id"]=> string(1) "2" } } [3]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(1) "3" ["name"]=> string(22) "evaluations" ["main_category_id"]=> string(1) "2" } } [4]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(1) "4" ["name"]=> string(11) "leave forms" ["main_category_id"]=> string(1) "2" } } [5]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(1) "5" ["name"]=> string(13) "payroll" ["main_category_id"]=> string(1) "2" } } [6]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(1) "6" ["name"]=> string(17) "recruitment" ["main_category_id"]=> string(1) "2" } } [7]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(1) "7" ["name"]=> string(24) "training" ["main_category_id"]=> string(1) "2" } } [8]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(1) "9" ["name"]=> string(13) "staff" ["main_category_id"]=> string(1) "2" } } [9]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(2) "10" ["name"]=> string(14) "codes" ["main_category_id"]=> string(2) "3" } } [10]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(2) "11" ["name"]=> string(28) "reports" ["main_category_id"]=> string(2) "3" } [11]=> array(1) { ["subcategory"]=> array(3) { ["id"]=> string(2) "12" ["name"]=> string(14) "plan" ["main_category_id"]=> string(2) "4" } } }

look in javascript:

var subcat[ ["accounting"], ["applications","benefit claims","evaluations","leave forms","payroll","recruitment","training","staff"], ["codes","reports"], ["plan"] ];

i tried couple of different php:

<?php $jsarray = array(); foreach($data $row) { $jsarray[] = array($row['subcategory']['name']); } echo json_encode($jsarray); ?>

also tried this:

<?php echo "["; foreach($data $row){ foreach($row $subcat) { echo "\"" . $subcat['name'] . "\","; } }echo "]"; ?>

what doing wrong?

like said above, pretty close json_encode example. need introduce sec dimension under main_category_id.

<?php $data = array( array("subcategory"=> array("id"=>"8", "name"=>"accounting","main_category_id"=>"1")), array("subcategory"=> array("id"=>"1", "name"=>"applications","main_category_id"=>"2")), array("subcategory"=> array("id"=>"2", "name"=>"benefit claims","main_category_id"=>"2")), array("subcategory"=> array("id"=>"3", "name"=>"evaluations","main_category_id"=>"2")), array("subcategory"=> array("id"=>"4", "name"=>"leave forms","main_category_id"=>"2")), array("subcategory"=> array("id"=>"5", "name"=>"payroll","main_category_id"=>"2")), array("subcategory"=> array("id"=>"6", "name"=>"recruitment","main_category_id"=>"2")), array("subcategory"=> array("id"=>"7", "name"=>"training","main_category_id"=>"2")), array("subcategory"=> array("id"=>"9", "name"=>"staff","main_category_id"=>"2")), array("subcategory"=> array("id"=>"10", "name"=>"codes","main_category_id"=>"3")), array("subcategory"=> array("id"=>"11", "name"=>"reports","main_category_id"=>"3")), array("subcategory"=> array("id"=>"12", "name"=>"plan","main_category_id"=>"4")), ); $selected = 7; $js = array(); foreach($data $sub){ //get parent id (main_category_id) $parent = $sub['subcategory']['main_category_id']; //if parent doesn't exist, add together if(!isset($js[$parent])){ //add array name , id $js[$parent] = array(array('id'=>$sub['subcategory']['id'],'name'=>$sub['subcategory']['name'])); //parent exist } else { //append entry name , id $js[$parent][] = array('id'=>$sub['subcategory']['id'],'name'=>$sub['subcategory']['name']); } } echo json_encode($js);

working: http://codepad.viper-7.com/nox9bt

here modified jsfiddle working output php above: http://jsfiddle.net/wprld/5/

php javascript arrays

No comments:

Post a Comment