Skip to content
Advertisement

How stop problem of overwriting in array using foreach php

public function getShoppingCartAbandonmentinfo($params){
    $final_array=array();
    require_once(DIR_WS_MODEL . "UserMaster.php");
    require_once(DIR_WS_MODEL . 'OrderExportMaster.php');
    require_once(DIR_WS_MODEL . 'BasketMaster.php');

    require_once (DIR_WS_MODEL . "/ProductsMaster.php");
    require_once (DIR_WS_MODEL . "/ProductPriceMaster.php");

    $objProductsMaster = new ProductsMaster();
    $objProductPriceMaster = new ProductPriceMaster();



    $ObjUserMaster            = new UserMaster();
    $UserData                = new UserData();

    $ObjBasketMaster        = new BasketMaster();
    $ObjBasketData            = new BasketData();

    $ObjOrderExportMaster    = new OrderExportMaster();
    $ObjOrderExportData        = new OrderExportData();


    define_const(PRODUCT_SEPARATOR, '||');

    $static_cust_array = array('firstname' => 'user_master.firstname AS first_name','lastname' => 'user_master.lastname AS last_name','companyname' => 'user_master.companyname AS company_name','email' => 'user_master.email','user_id' => 'user_master.userid','customer_type'=>'user_master.customer_type AS guest_customer');
    $ObjBasketMaster->setSelect("user_master.userid")->setSelect((array)$static_cust_array);
    $ObjBasketMaster->setJoin("LEFT JOIN user_master ON user_master.userid=user_basket.user_id");
    $customer_details = $ObjBasketMaster->getBasket();
    foreach ($customer_details as $customer_field_value){
        $output='';
        foreach ($customer_field_value as $field_key => $customer_details_value){

           if($field_key == 'guest_customer' ){
                $field_key ="guest_user";
                if($customer_details_value == 1){
                    $customer_details_value = "NO";
                }else{
                    $customer_details_value = "YES";
                }
            }
            $output[$field_key]= $customer_details_value;
            $all['customer_details']=$output;
        }
        $output1[]= $all;
    }
    //echo "<pre>";print_r($output1);echo "</pre>";exit;

    /*Shopping Cart Loop  */
    $static_basket_array=array('basket_id'=>'user_basket.basket_id','user_basket.date'=>'date AS created_date','cart_detail'=>'user_basket.cart_detail AS products');
    $ObjBasketMaster->setSelect((array)$static_basket_array);
    $ObjBasketMaster->setSelect('user_basket.date AS abandoned_days');
    $ObjBasketMaster->setJoin("LEFT JOIN user_master ON user_master.userid=user_basket.user_id");
    $cart_details = $ObjBasketMaster->getBasket();

    foreach ($cart_details  as $cart_field_value){
        $output='';
        foreach ($cart_field_value as $field_key => $cart_details_value){

            if($field_key == 'abandoned_days' ){
                $OldDate = $cart_details_value;
                $now = time(); // or your date as well
                $your_date = strtotime($OldDate);
                $datediff = $now - $your_date;
                $cart_details_value=round($datediff / (60 * 60 * 24));
            }
            if($field_key == 'products' && !empty($cart_details_value) ){
                $cart_data=unserialize($cart_details_value);
                foreach ($cart_data as $key => $data){
                    $objProductsMaster->setSelect('products_title');
                   $objProductsMaster->setWhere("AND products_id = :products_id",$data['product_id'],'int');
                   $objProductsMaster->setWhere('AND site_language_id = :site_language_id', SITE_LANGUAGE_ID, 'int');
                   $datad = $objProductsMaster->getProductsDesc();
                   $products= array('product_id'=>$data['product_id'],'product_title'=>$datad[0]['products_title']);
                }
                $cart_details_value=array();
                $cart_details_value= $products;

            }

            $output[$field_key]= $cart_details_value;
            $all['shopping_cart']=$output;
        }
        echo "<pre>";print_r($output1);echo "</pre>";exit;
        $output1[]= $all;
    } 

    /*End OF Shopping Cart  */

     if(empty($output1))
        $output=array('Message'=>NO_RECORD_FOUND);

        return $output1;


}

}

Here in $output1 i always get the latest data, but i want all the data . The data is been over written in $output1 .

So how can i add $customer_details_value as well as $cart_details_value both in $output1.

Here after adding $cart_details_value in $output it over rides value of $customer_details_value in $output1

Advertisement

Answer

You need to define your $output1 before the loops:

$output1 = [];

And better start using informative variable names, to easy read the code and find the errors.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement