0

I am trying to get (id_product & cart_id) for every product in below array object.

 Order Object
    (
        [product_list] => Array
            (
                [0] => Array
                    (
                        [id_product_attribute] => 14923
                        [id_product] => 2024
                        [cart_quantity] => 1
                        [id_shop] => 1
                        [id_customization] => 
                        [name] => Glitch Peacock Optimise A.I.R. Full Zip Triathlon Suit - Men
                        
    
                    )
                    [1] => Array
                    (
                        [id_product_attribute] => 14925
                        [id_product] => 2025
                        [cart_quantity] => 1
                        [id_shop] => 1
                        [id_customization] => 
                        [name] => Full Zip Triathlon Suit - Men
                        
    
                    )
    
            )
    
    )

I am not sure what is the optimized way to get data. Any thoughts ? Thanks

  • If not for simple looping, then you could do `array_column($obj->product_list, 'cart_quantity', 'id_product')`, which would give you an array with `id_product` as the index and `cart_quantity` as the value. See [array_column](https://www.php.net/array_column). – Markus AO Dec 31 '20 at 11:14
  • Thankyou very much, let me try in code. I want to save this data for each product in array so that i can use it. –  Dec 31 '20 at 11:15
  • @MarkusAO the format is \[\`some code\`\](the link). Backticks go inside the brackets :) – Cid Dec 31 '20 at 11:16
  • @Cid already edited, I tried to link the function *inside* a part of the backticked code, which seems not to be supported. I suppose we have to do [`array_column`](https://www.php.net/array_column)`($obj->product_list...)` instead and split the ticky bits. I see that it results in an icky extra space. – Markus AO Dec 31 '20 at 17:21

1 Answers1

0

Your product_list is an array inside of an Order object. Just iterate over it and get any fields you need.

<?php

class Order
{
    public $product_list = [];
}

$order               = new Order();
$order->product_list = [
    [
        'id_product_attribute' => 14923,
        'id_product'           => 2024,
        'cart_quantity'        => 1,
        'id_shop'              => 1,
        'id_customization'     => '',
        'name'                 => 'Glitch Peacock Optimise A.I.R. Full Zip Triathlon Suit - Men',
    ],
    [
        'id_product_attribute' => 14925,
        'id_product'           => 2025,
        'cart_quantity'        => 1,
        'id_shop'              => 1,
        'id_customization'     => '',
        'name'                 => 'Full Zip Triathlon Suit - Men',
    ],
];

foreach ($order->product_list as $product) {
    var_dump($product['id_product']);
}
Mikhail Prosalov
  • 4,155
  • 4
  • 29
  • 41
  • is their any way to get id product without creating class ? –  Jan 04 '21 at 08:30
  • I believe you can get it from the `$product['id_product']` value while iterating your product list. – Mikhail Prosalov Jan 04 '21 at 09:05
  • Oh, I know what you mean, you don't need to create a class, as you already have one. I have created a class just for sake of example. Just iterate over your existing Order object. – Mikhail Prosalov Jan 04 '21 at 09:24