Solved

How to get correct json output in this case?

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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