从PHP数组创建二维javascript数组

时间:2013-02-11 22:29:55

标签: php javascript arrays

如何制作这个PHP数组:

 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" } } 
    }

在javascript中看起来像这样:

var SubCat[
["Accounting"],
["Applications","Benefit Claims","Evaluations","Leave Forms","Payroll","Recruitment","Training","Staff"],
["Codes","Reports"],
["Plan"]
];

我尝试了几个不同的php:

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

也尝试了这个:

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

就像我上面说过的那样,你的json_encode示例非常接近。您只需要在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 as $sub){
    //get the parent id (main_category_id)
    $parent = $sub['SubCategory']['main_category_id'];
    //if the parent doesn't exist, add it
    if(!isset($js[$parent])){
        //add array with name and id
        $js[$parent] = array(array('id'=>$sub['SubCategory']['id'],'name'=>$sub['SubCategory']['name']));
    //parent does exist
    } else {
        //append this entry name and id
        $js[$parent][] = array('id'=>$sub['SubCategory']['id'],'name'=>$sub['SubCategory']['name']);
    }
}

echo json_encode($js);

工作:http://codepad.viper-7.com/NOX9bT

以下是修改过的jsfiddle,它使用上面php的输出:http://jsfiddle.net/wprLD/5/