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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:46:43+00:00 2026-06-10T15:46:43+00:00

Possible Duplicate: Casting an Array with Numeric Keys as an Object I was wondering

  • 0

Possible Duplicate:
Casting an Array with Numeric Keys as an Object

I was wondering about (object) type casting.

It’s possible to do a lot of useful things, like converting an associative array to an object, and some not so useful and a bit funny (IMHO) things, like converting a scalar value to object.

But how can I access the result of casting of an indexed array?

// Converting to object an indexed array
$obj = (object) array( 'apple', 'fruit' );

How about accessing a specific value?

print $obj[0];      // Fatal error & doesn't have and any sense
print $obj->scalar[0];  // Any sense
print $obj->0;      // Syntax error
print $obj->${'0'};     // Fatal error: empty property.   
print_r( get_object_vars( $obj ) ); // Returns Array()

print_r( $obj );    /* Returns
                    stdClass Object
                     (
                            [0] => apple
                            [1] => fruit
                     )
                    */

The following works because stdClass implements dynamically Countable and ArrayAccess:

foreach( $obj as $k => $v ) {
    print $k . ' => ' . $v . PHP_EOL;
}  
  • 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-10T15:46:45+00:00Added an answer on June 10, 2026 at 3:46 pm

    This is actually a reported bug.

    It has been deemed as “too expensive to fix” and the resolution has
    been “updated the documentation to describe this useless quirk, so it
    is now officially correct behavior” [1].

    However, there are some workarounds.

    As get_object_vars gives you nothing, only thing you can do is:

    1. You can iterate on the stdClass using foreach
    2. You can cast it back as array.
    3. You can change cast it to object using json_decode+json_encode (this is dirty trick)

    Example 1.:

    $obj = (object) array( 'apple', 'fruit' );
    foreach($obj as $key => $value) { ...
    

    Example 2.:

    $obj = (object) array( 'apple', 'fruit' );
    $array = (array) $obj;
    echo $array[0];
    

    Example 3.:

    $obj = (object) array( 'apple', 'fruit' );    
    $obj = json_decode(json_encode($obj));    
    echo $obj->{'0'};
    var_dump(get_object_vars($obj)); // array(2) {[0]=>string(5) "apple"[1]=>string(5)"fruit"}
    

    This is why you shouldn’t cast non-associative array as object 🙂

    But if you want to, do it in this way:

    // PHP 5.3.0 and higher
    $obj = json_decode(json_encode(array('apple', 'fruit'), JSON_FORCE_OBJECT));
    // PHP 5 >= 5.2.0
    $obj = json_decode(json_encode((Object) array('apple', 'fruit')));
    

    instead of

    $obj = (Object) array('apple','fruit'); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Casing an Array with Numeric Keys as an Object I made a
Possible Duplicate: casting char[][] to char** causes segfault? I have a 2D array declared
Possible Duplicate: Casting: (NewType) vs. Object as NewType Casting vs using the 'as' keyword
Possible Duplicate: Casting: (NewType) vs. Object as NewType Say for example I have a
Possible Duplicate: Casting vs using the ‘as’ keyword in the CLR Which method is
Possible Duplicates: Casting: (NewType) vs. Object as NewType Why is the C# “as” operator
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: casting vs using the ‘as’ keyword in the CLR hi, can somebody
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always

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.