Search code examples
phpmysqlsqlarraysassociative-array

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

  • 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


    I also created a recursive function that converts an array to or from an stdClass by reference:

    function array_to_stdClass(&$array){
        $tmp = $array;
        $array = new stdClass();
        foreach($tmp as $k => $v) $array->$k = (!is_object($v) && !is_array($v)) ? $v:array_to_stdClass($v);
        return $array;
    }
    
    function stdClass_to_array(&$stdClass){
        $tmp = $stdClass;
        $stdClass = array();
        foreach($tmp as $k => $v) $stdClass[$k] = (!is_object($v) && !is_array($v)) ? $v:stdClass_to_array($v);
        return $stdClass;
    }
    

    Online Sandbox Example: https://onlinephp.io/c/4b183