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 8343939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:12:57+00:00 2026-06-09T06:12:57+00:00

Currently I am trying to restructure a multidimensional array that is the result of

  • 0

Currently I am trying to restructure a multidimensional array that is the result of a database query. The goal is to finally encode the array as JSON as follows, but the php function i have written isn’t working (see code at end):

desiredJSONoutput = {"TypeA":[
    {"1":[
        {"TypeB":[4,5]},
        {"TypeC":[7,8,4]}]},
    {"2":[
        {"TypeB":[2,3]},
        {"TypeC":[6]}]},
    {"4":[
        {"TypeB":[33,12]},
        {"TypeC":[908]}]}]}

Database structure is:

fromType    fromID      toType      toID
-----------------------------------------------------
TypeA       1       TypeB       4
TypeA       1       TypeB       5
TypeA       1       TypeC       7
TypeA       1       TypeC       8
TypeA       1       TypeC       4
TypeA       2       TypeB       2
TypeA       2       TypeB       3
TypeA       2       TypeC       6
TypeA       2       TypeB       33
TypeA       2       TypeB       12
TypeA       2       TypeC       908

the result of my current sql query is this php array:

Array
(
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '1'
        ['toType'] => 'TypeB'
        ['toID'] => '4'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '1'
        ['toType'] => 'TypeB'
        ['toID'] => '5'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '1'
        ['toType'] => 'TypeC'
        ['toID'] => '7'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '1'
        ['toType'] => 'TypeC'
        ['toID'] => '8'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '1'
        ['toType'] => 'TypeC'
        ['toID'] => '4'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '2'
        ['toType'] => 'TypeB'
        ['toID'] => '2'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '2'
        ['toType'] => 'TypeB'
        ['toID'] => '3'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '2'
        ['toType'] => 'TypeC'
        ['toID'] => '6'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '3'
        ['toType'] => 'TypeB'
        ['toID'] => '33'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '3'
        ['toType'] => 'TypeB'
        ['toID'] => '12'  
    )
    Array
    (
        ['fromType'] => 'TypeA'
        ['fromID'] => '3'
        ['toType'] => 'TypeC'
        ['toID'] => '908'  
    )
)

and the restructured array i need before encoding to JSON I think is:

Array
    (
        ['TypeA'] => Array
            (
                ['1'] => Array
                    (
                        ['TypeB'] => Array
                            (
                                [0] => 4
                                [1] => 5
                            )
                        ['TypeC'] => Array
                            (
                                [0] => 7
                                [1] => 8
                                [2] => 4
                            )
                    )
                ['2'] => Array
                    (
                        ['TypeB'] => Array
                            (
                                [0] => 2
                                [1] => 3
                            )
                        ['TypeC'] => Array
                            (
                                [0] => 6
                            )
                    )
                ['3'] => Array
                    (
                        ['TypeB'] => Array
                            (
                                [0] => 33
                                [1] => 12
                            )
                        ['TypeC'] => Array
                            (
                                [0] => 908
                            )
                    )
            )  
    )

I am not sure why the PHP code as follows is not doing the trick to restructure the returned php array to the structure I want:

class Utilities {
    public function linksReEncode($rowsArray) {
        $result = array();

        foreach ($rowsArray as $row) {

            $fromtype = $row['fromtype'];
            $fromID = $row['fromID'];
            $toType = $row['toType'];
            $toID = $row['toID'];

            $arr = $result[$fromType][$fromID][$toType];

            array_push($arr, $toID);
        }
    }
}

EDIT
based on further research and the first answer from @Waygood here’s the function that i am now using and which is working. I wonder if all the checking for existing keys is necessary and if this is the most economical way to achieve this

public function linksReEncode($rows) {
        $result = array();

        foreach ($rows as $row) {

            $fromType = $row['fromType'];
            $fromID = $row['fromID'];
            $toType = $row['toType'];
            $toID = $row['toID'];

            if(!array_key_exists($fromType, $result))
                $result[$fromType] = array();

            if(!array_key_exists($fromID, $result[$fromType]))
                $result[$fromType][$fromID] = array();

            if(!array_key_exists($toType, $result[$fromType][$fromID]))
                $result[$fromType][$fromID][$toType] = array();

            array_push($result[$fromType][$fromID][$toType], $toID);

        }
        return $result;
    }
  • 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-09T06:12:59+00:00Added an answer on June 9, 2026 at 6:12 am

    you are just redefining $arr within each loop, not altering $result

    change:

    $arr = $result[$fromType][$fromID][$toType];
    array_push($arr, $toID);
    

    to:

    $result[$fromType][$fromID][$toType][]=$toID;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well i am currently trying to do a query in SQL, but i wanna
Currently trying at add ajax to a site, after much reading I discovered that
I am currently trying to write some code that will accept some FTP details,
I'm currently trying to get the most popular productID from my MSSQL Database. This
I´m currently trying to add the feature that is discussed here: http://blog.bmn.name/2008/03/jquery-fadeinfadeout-ie-cleartype-glitch/ A glitch
I am currently trying to write an algorithm that determines how many bits are
I'm currently trying to group messages that are sent out by 1 second time
Currently trying to come up with a best practice for a ZF site that
I am currently trying to read in a log file that consists of [content].
I am currently trying to split one value with percentage column. But as most

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.