'Using Associative arrays

I'm trying to access a position of a associative array which is inside of another array.

Here is my array:

    Array
(
    [order_data] => stdClass Object
        (
            [id_order] => 2
            [id_client] => 1
            [date_order] => 2022-04-03 18:43:18
            [address] => meu puto
            [city] => Rabinho
            [email] => [email protected]
            [cellphone] => 919788427
            [code_order] => QX669156
            [status] => PENDIG
            [message] => 
            [created_at] => 2022-04-03 18:43:18
            [updated_at] => 2022-04-03 18:43:18
        )

    [products_order] => Array
        (
            [0] => stdClass Object
                (
                    [id_order_product] => 8
                    [id_order] => 2
                    [name_product] => Green Tshirt
                    [price_unity] => 35.15
                    [quantity] => 1
                    [created_at] => 2022-04-03 18:43:18
                )

        )

)

Im trying to access the field which name is name_product and this is my code in order to do that:

$image = $order_details['products_order']['name_product']);

and this gives me the following error

Undefined array key "name_product"

probably it's just a simple thing.



Solution 1:[1]

You cannot access your data like your normally would an array, as said by @Omar_Tammam you are using Array objects.

Here is an example of how your data is set up and how you could use it:

    # Creating your stdclass Object Array
    $order_details['order_data'] = new stdClass();
    $order_details['order_data']->id_order = 2;
    $order_details['order_data']->id_client = 1;
    $order_details['order_data']->date_order = "2022-04-03 18:43:18";
    $order_details['order_data']->address = "meu puto";
    $order_details['order_data']->city = "Rabinho";
    $order_details['order_data']->email = "[email protected]";
    $order_details['order_data']->cellphone = "919788427";
    $order_details['order_data']->code_order = "QX669156";
    $order_details['order_data']->status = "PENDING";
    $order_details['order_data']->message = "";
    $order_details['order_data']->created_at = "2022-04-03 18:43:18";
    $order_details['order_data']->updated_at = "2022-04-03 18:43:18";
    $order_details['products_order'][0] = new stdClass();
    $order_details['products_order'][0]->id_order_product = "8";
    $order_details['products_order'][0]->id_order = "2";
    $order_details['products_order'][0]->name_product = "Green Tshirt";
    $order_details['products_order'][0]->price_unity = "35.15";
    $order_details['products_order'][0]->quantity = "1";
    $order_details['products_order'][0]->created_at = "2022-04-03 18:43:18";
    
    # To print all the data as example, showing stdClass Objects can 
    # be accessed through a foreach similar to an array
    // foreach($order_details as $od){
    //     echo PHP_EOL, "_________________________", PHP_EOL;
    //     foreach($od as $a){
    //         if(!is_object($a)) echo $a, PHP_EOL;
    //         else foreach($a as $o) echo $o, PHP_EOL;
    //     }
    //     echo PHP_EOL, "_________________________", PHP_EOL;
    // }
    
    # this is wrong & will not work:
    echo $order_details['products_order'][0]['name_product'];
    
    # you would want to use this:
    echo $order_details['products_order'][0]->name_product;
    # more examples:
    echo $order_details['order_data']->address;
    echo $order_details['order_data']->city;
    echo $order_details['order_data']->email;
    echo $order_details['products_order'][0]->created_at;

    # or as you wanted:
    $image = $order_details['products_order'][0]->name_product;

    echo $image;
    # result would be: Green Tshirt

Online Sandbox Example: https://onlinephp.io/c/76370

Solution 2:[2]

the structure of the data is an array of array of object

this should works with you :

$image = $order_details['products_order'][0]->name_product;

Solution 3:[3]

$image = $order_details['products_order'][0]);

Solution 4:[4]

See the [0] in [0] => stdClass Object? The following should work ...

$image = $order_details['products_order'][0]['name_product'];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Gev99
Solution 4 neokio