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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:35:31+00:00 2026-06-05T14:35:31+00:00

I have array converted into stdClass object like this, stdClass Object ( [1339697186] =>

  • 0

I have array converted into stdClass object like this,

stdClass Object
(
    [1339697186] => stdClass Object
        (
            [1403873546800880] => stdClass Object
                (
                    [quantity_request] => 2
                    [time_created] => 1339697190
                    [variant] => stdClass Object
                        (
                            [0] => 1403873546800887
                        )

                )

        )

    [1339697196] => stdClass Object
        (
            [1403873546800880] => stdClass Object
                (
                    [quantity_request] => 1
                    [time_created] => 1339697196
                    [variant] => stdClass Object
                        (
                            [0] => 1403889656952419
                        )

                )

        )

)

So if I want to get [quantity_request] of each item, I will loop twice to get the answer,

foreach ($items as $key => $item) 
{
    foreach ($item as $code => $item) 
    {
        echo $item->quantity_request;
    }
}

I want to know if there is a way to get the answer like this below without looping the object array twice?

foreach ($items as $key => $item) 
{
    # Get the product code of this item.
    $code = $cart->search_code($key);

    echo $item->$code->quantity_request;
}

error:

Fatal error: Cannot use object of type stdClass as array in…

I have a method in a class to get the code (sub key) from the content of the object array.

public function search_code($key)
{
        # Get this item.
        $item = $this->content[$key];

        # Get this item's sub key, which is the code of the product.
        $subkeys = array_keys($item);

        # Get the first item from the array.
        $code = $subkeys[0];

        # Return the sub key which is the code of the product.
        return $code;
}
  • 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-05T14:35:33+00:00Added an answer on June 5, 2026 at 2:35 pm

    Yes, that I know of :

    foreach ($items as $key => $item) 
    {
        $array = (object) array_shift($item);
    
        echo $array->quantity_request.'<br />';
    }
    

    I hope I answer your question, didn’t understand well at 100%.

    EDIT

    <?php
    $items = (object) array(
        1339697186 => array(1403873546800880 => array('quantity_request' => 2)),
        1339697187 => array(1403873546800880 => array('quantity_request' => 3)),
        1339697188 => array(1403873546800880 => array('quantity_request' => 4))
    );
    
    foreach ($items as $key => $item) 
    {
        $array = (object) array_shift($item);
    
        echo $array->quantity_request.'<br />';
    }
    ?>
    
    // Results :
    2<br />3<br />4<br />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have converted an array to object data like this: <?php $myobject->data = (object)Array('zero','one','two');
I have a string that has been converted into an 2D Array in js.
Using explode i have converted the string into array , But the array look
Here's the problem. I ,for example,have a string 2500.Its converted from byte array into
I have a HashMap which I want to convert into a array. Which the
I have the following function to convert a byte array of pixels into an
I have taken char data into database into array. now i want to convert
I have a double converted to a unsigned char array. For example for value
For a project, I have to convert a binary string into (an array of)
Hi I have a json string converted unsing the JSON framework into a dictionary

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.