Using Associative arrays



PHP Snippet 1:

    # 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

PHP Snippet 2:

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

PHP Snippet 3:

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

PHP Snippet 4:

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