Solved

How to get correct json output in this case?

Posted on 2011-09-10
3
245 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 33

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now