Solved

How to get correct json output in this case?

Posted on 2011-09-10
3
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

635 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