Solved

How to get correct json output in this case?

Posted on 2011-09-10
3
250 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:3xtr3m3d
3 Comments
 
LVL 34

Accepted Solution

by:
Slick812 earned 250 total points
ID: 36517534
greetings 3xtr3m3d,  There is a difference (I would say a BIG difference) in the way javascript uses arrays and Objects compared to how PHP uses element references inside the [ ]  . The  PHP json_encode( ) does a "text" conversion of PHP arrays and objects to javascript arrays and objects, which are Different than PHP stuff. What you show as the
{"data":{"0":{"
seems correct to me, if you use this in javascript, it will likely give you the same data structures and values that were in the PHP. In javascript arrays can not have text (strings) as elements between the [ ]  . however you can access object elements with data.ASCM72X60 or data['ASCM72X60']
do you have problems in the javascript access for the data object ?
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 250 total points
ID: 36518012
right BEFORE:

echo json_encode($responce);


try:
...
$responce=(array)$responce;
echo json_encode($responce);
...
0
 
LVL 3

Author Comment

by:3xtr3m3d
ID: 36518302
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

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question