Link to home
Start Free TrialLog in
Avatar of 3xtr3m3d
3xtr3m3d

asked on

How to get correct json output in this case?

Hi

this is the $distribution array

    Array
    (
        [ASCM72X36] => Array
            (
                [item_code] => ASCM72X36
                [quantity] => 5
                [selling_price] => 6758.00
            )
    
        [ASCM72X48] => Array
            (
                [item_code] => ASCM72X48
                [quantity] => 5
                [selling_price] => 
            )
    
        [ASCM72X60] => Array
            (
                [item_code] => ASCM72X60
                [quantity] => 5
                [selling_price] => 8544.00
            )
    )

Open in new window


and this is the $sold array

        Array
        (
            [ASCM72X36] => Array
                (
                    [item_code] => ASCM72X36
                    [quantity] => 1.0
                )
        
            [ASCM72X60] => Array
                (
                    [item_code] => ASCM72X60
                    [quantity] => 1.0
                )
    ) 

Open in new window


so im comparing keys and building new $responce array with new quantity and filter out quantity 0 products like below

        $i=0;
        foreach($distribution as $key => $new_distribution)
        {
          $newqty = $new_distribution['quantity'] - $sold[$key]['quantity'];
          if( $newqty != 0 && $new_distribution['selling_price'] != ""){
            $responce->data[$i]['item_code'] = $new_distribution['item_code'];
            $responce->data[$i]['quantity'] = $newqty;
            $responce->data[$i]['selling_price'] = $new_distribution['selling_price'];
          }
        $i++;
    
    }

Open in new window


then i need to get json encode out put so im doing it like this

    echo json_encode($responce);

Open in new window


im getting out put like

    {"data":{"0":{"item_code":"ASCM72X36","quantity":4,"selling_price":"6758.00"},"2":{"item_code":"ASCM72X60","quantity":4,"selling_price":"8544.00"}}}

Open in new window


problem is im getting a "0", "2" etc.. in json. how to prevent that and get the out put like without those "0" s and "2" etc...?

    {"data":{"item_code":"ASCM72X36","quantity":4,"selling_price":"6758.00"},{"item_code":"ASCM72X60","quantity":4,"selling_price":"8544.00"}}

Open in new window



Sorry for the bad English.


Regards
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 3xtr3m3d
3xtr3m3d

ASKER

Thanks for the replies guys. that didn't remove array indices though.

@Slick812: ya with those indices im having problem loading a Extjs store.

anyway this solved my problem

                    $arr=array();
                    $arr['item_code'] = $new_distribution['item_code'];
                    $arr['quantity'] = $newqty;
                    $arr['selling_price'] = $new_distribution['selling_price'];
                    $responce->data[] = $arr;

Open in new window


Thanks