Home > Software design >  element not added when push in array
element not added when push in array

Time:06-17

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
        }   
    }

CodePudding user response:

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;
    }
}
  • Related