0

How to make this 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 like this in javascript:

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

I tried a couple of different php:

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

also tried this:

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

What am I doing wrong?

3
  • At first glance, your json_encode approach looks actually correct. Could you please post the output of both your attempts? Commented Feb 11, 2013 at 22:32
  • First of all, definitely go the json_encode way, for loads of reasons. Secondly, I do not understand the input format. It looks like some sort of serialized PHP? Commented Feb 11, 2013 at 22:34
  • The json_encode is pretty close to correct, except that you also need to add another dimension that is the main_category_id. Also, one small problem, in the data provided, the last entry has main_category_id as 4, but the expected result has it grouped with 3. Commented Feb 11, 2013 at 22:44

1 Answer 1

1

Like I said above, you were pretty close with your json_encode example. You just need to introduce the second dimension under the 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);

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

Here is the modified jsfiddle working with the output from the php above: http://jsfiddle.net/wprLD/5/

Sign up to request clarification or add additional context in comments.

5 Comments

I also need to grab the SubCategory id and place it in as <option value='SubCategory.id'>SubCategory.name</option>, any ideas on that?
@sloga I updated the code to store a list of options that can just be echo'ed between a <select> tag.
I should show you where I'm headed with this. In my function makeSubCatHtml(catId), I need to put the SubCategory.id into the SubCatHtml += <option value=''>....jsfiddle.net/wprLD/1
Well then you are probably going to want to make each appended name into another array with both id and name. I'll update.
Thanks for the update. I tweaked it a bit because the index was off and have it working at jsfiddle.net/wprLD/9 - very much appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.