element not added when push in array

I’m trying to push an array into other array but it does not work. Also, i have tried with array_push and does not work

    foreach($cartitemCollection as $cartitem){
        foreach($cartitem['product'] as $product){
            $variablePrices = $variablePricesRepository->findByProductId($product['id']);
            $product['var_price'] = $variablePrices; //not working
            array_push( $product,$variablePrices ); //also not working
        }   
    }

>Solution :

On next iteration product will be overwritten. But you can use a reference. Note the ampersand.

foreach($cartitemCollection as $cartitem){
    foreach($cartitem['product'] as &$product){
        $variablePrices = $variablePricesRepository->findByProductId($product['id']);
        $product['var_price'] = $variablePrices;
    }
}

Leave a Reply