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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:19:19+00:00 2026-06-15T11:19:19+00:00

Possible Duplicate: convert object to array Let’s say I have an array like this:

  • 0

Possible Duplicate:
convert object to array

Let’s say I have an array like this: (Notice that some of the methods/objects may be protected, so they have to be accessed in their own class)

array(
    0=> objectname{
        [method1:protected]=> array(
            ["key1"] => object2{
                [method2]=> array(
                    0 => "blah"
                )
            }
        )
    }
    1=> objectname{
        [method1:protected]=> array(
            ["key1"] => object2{
                [method2]=> array(
                    0 => "blah"
                )
            }
        )
    }
)

And I wanted to convert all of that into an Array. I would usually use this:

protected function _object_to_array($obj){

    if(is_object($obj)) $obj = (array) $obj;

    if(is_array($obj)) {

        $new = array();
        foreach($obj as $key => $val) {
            $new[$key] = self::_object_to_array($val);
        }

    }else{

        $new = $obj;

    }

    return $new;

}

The problem is that this does not preserve the object names. I would like the object names to become an extra key that bumps the array up a dimension. For example, replacing the 0 for objectname could work, but better yet to create something like this:

array(
    0=> array(
        objectname=> array(
            ...blah blah
        )
    )
)
  • 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-15T11:19:20+00:00Added an answer on June 15, 2026 at 11:19 am

    Figured it out.

    However new problem, the protected methods endup as keys like [*formermethodturnedkey]. They don’t seem to be accessible. How can one access keys like this?

    protected function _object_to_array($obj){
    
        //we want to preserve the object name to the array
        //so we get the object name in case it is an object before we convert to an array (which we lose the object name)
        $obj_name = false;
        if(is_object($obj)){
            $obj_name = get_class($obj);
            $obj = (array) $obj;
        }
    
        //if obj is now an array, we do a recursion
        //if obj is not, just return the value
        if(is_array($obj)) {
    
            $new = array();
    
            //initiate the recursion
            foreach($obj as $key => $val) {
                //we don't want those * infront of our keys due to protected methods
                $new[$key] = self::_object_to_array($val);
            }
    
            //now if the obj_name exists, then the new array was previously an object
            //the new array that is produced at each stage should be prefixed with the object name
            //so we construct an array to contain the new array with the key being the object name
            if(!empty($obj_name)){
                $new = array(
                    $obj_name => $new,
                );
            }
    
        }else{
    
            $new = $obj;
    
        }
    
        return $new;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Convert multidimensional array into single array I have this array: Array (
Possible Duplicate: Convert flat array [k1,v1,k2,v2] to object {k1:v1,k2:v2} in JavaScript? I want to
Possible Duplicate: how to convert java string to Date object I have a form
Possible Duplicate: Can’t convert value type array to params object[] Why is object h
Possible Duplicate: Casting: (NewType) vs. Object as NewType Say for example I have a
Possible Duplicate: Find object by id in array of javascript objects So I have
Possible Duplicate: Convert JS object to JSON string I have a JSON object in
Possible Duplicate: How to convert MDB to SQLite in Android I have an MS
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want

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.