Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:41:22+00:00 2026-06-18T15:41:22+00:00

How to make this PHP array: array(12) { [0]=> array(1) { [SubCategory]=> array(3) {

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:41:23+00:00Added an answer on June 18, 2026 at 3:41 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an PHP array: array 'ItemId' => array 0 => string '32' (length=2)
I make this php program to access mysql data foreach ($arrayOfID as $ID) {
I'm trying to make this: 174,myfilename.jpg Into this on the php post process page:
Ho to make this simple xml with php using DOM? Full code will be
I need to make this data variable global: $.ajax({ url: get_data.php, cache: false, dataType:
I want to make this form: echo '<form method=post action=ratinghandler.php style=width: 250px> <input type=hidden
How can I make something like this below work? <?PHP $_SESSION['signup_errors']['test1']; $_SESSION['signup_errors']['test2']; $_SESSION['signup_errors']['test3']; $_SESSION['signup_errors']['test4'];
Every time in PHP when I make a variable such as this one: $date
how to make php can read log file in /var/log/.... I tried this code
I wanted to make a post request in this website: http://www.prezup.info/index.php?page=films in order to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.