Asked  1 Year ago    Answers:  5   Viewed   14 times

Goal: retrieve an element of data from within a PHP object by number.

This is the print_r($data) of the object:

stdClass Object
    [0] => stdClass Object
            [TheKey] => 1456
            [ThingName] => Malibu
            [ThingID] => 7037
            [MemberOf] => California
            [ListID] => 7035
            [UserID] => 157
            [UserName] => John Doe

I can't figure out how to pull a value out of it. This is only one record of a multi-record object that should be by id rather than a name.

These are some failed attempts to illustrate what the goal is:

echo $data -> 0 -> UserName;
echo $data[0] -> UserName;



Normally, PHP variable names can't start with a digit. You can't access $data as an array either as stdClass does not implement ArrayAccess — it's just a normal base class.

However, in cases like this you can try accessing the object attribute by its numeric name like so:

echo $data->{'0'}->UserName;

The only reason I can think of why Spudley's answer would cause an error is because you're running PHP 4, which doesn't support using foreach to iterate objects.

Thursday, April 1, 2021

Another easy way is to use the WC_Product_Factory class and then call function get_product(ID)


// assuming the list of product IDs is are stored in an array called IDs;
$_pf = new WC_Product_Factory();  
foreach ($IDs as $id) {

    $_product = $_pf->get_product($id);

    // from here $_product will be a fully functional WC Product object, 
    // you can use all functions as listed in their api

You can then use all the function calls as listed in their api:

Thursday, April 1, 2021

Keep always in mind that a very powerful feature of PHP is its Variable Variables

You can use

$attr = 'field' . $key;

or more concisely, using curl brackets

Saturday, May 29, 2021

As BoltClock mentioned there is no object literal in PHP however you can do this by simply type casting the arrays to objects:

$testArray = array(
    (object)array("name" => "John", "hobby" => "hiking"),
    (object)array("name" => "Jane", "hobby" => "dancing")

echo "Person 1 Name: ".$testArray[0]->name;
echo "Person 2 Hobby: ".$testArray[1]->hobby;
Sunday, August 8, 2021

It will be destructed (unloaded from memory) at the end of the page load, or if you unset all references to it earlier. You will not have to destroy it manually since PHP always cleans up all memory at the end of the script.

In fact, you should never call __destruct yourself. Use unset to unset the reference to an object when you want to destroy it. __destruct will in fact not destroy your object, it's just a function that will get called automatically by PHP just before the destruction so you get a chance to clean up before it's destroyed. You can call __destruct how many times as you want without getting your memory back.

If, however, you've saved the object to a session variable, it will "sleep" rather than be destroyed. See the manual for __sleep. It will still be unloaded from memory (and saved to disk) of course since PHP doesn't hold anything in memory between scripts.

Tuesday, October 12, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :