0

如何制作这个 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 "]";
?>

我究竟做错了什么?

4

1 回答 1

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/

于 2013-02-11T22:50:59.920 回答