不使用密钥创建PHP对象?

I've got a request to present the data in the following format as a JSON feed:

{
    "id": "123",
    "info": {
        "code": "ZGE",
        "description": "test1",
        "type": "AVL",
        "date": "09/08/2012"
    }
},
{
   "id": "456",
    "info": {
        "code": "ZDN",
        "description": "test2",
        "type": "CLR",
        "date": "16/02/2012"
    }
}

However in my PHP code, I think I need to have a key itterator - but I end up with this format:

{

"0": {
    "id": "123",
    "info": {
        "code": "ZGE",
        "description": "test1",
        "type": "AVL",
        "date": "09/08/2012"
    }
},
"1": {

    "id": "456",
    "info": {
        "code": "ZDN",
        "description": "test2",
        "type": "CLR",
        "date": "16/02/2012"
    }
}
}

Any ideas on how to build the first data set with out having the index iterator?

It can be built like this:

$arr = array(
    array(
        'id' => 123,
        'info' => array(
            'code' => 'ZGE',
            'description' => 'test1',
            'type' => 'AVL'
        )
    ),
    array(
        'id' => 456,
        'info' => array(
            'code' => 'ZDN',
            'description' => 'test2',
            'type' => 'CLR'
        )
    )
);

echo json_encode($arr);

Outputs

[
    {
        "id": 123,
        "info": {
            "code": "ZGE",
            "description": "test1",
            "type": "AVL"
        }
    },
    {
        "id": 456,
        "info": {
            "code": "ZDN",
            "description": "test2",
            "type": "CLR"
        }
    }
]

simple create an array of objects, no need for the key (notice the [ ] surrounding your list)

json.txt

[{
    "id": "123",
    "info": {
        "code": "ZGE",
        "description": "test1",
        "type": "AVL",
        "date": "09/08/2012"
    }
},
{
   "id": "456",
    "info": {
        "code": "ZDN",
        "description": "test2",
        "type": "CLR",
        "date": "16/02/2012"
    }
}]

example.php

<?php
    $data = json_decode(file_get_contents('./json.txt'));
?>

The only reason json_encode should produce the output you're seeing is adding another named key to the array that you're passing to json_encode, by default it should work as you want:

$json = '[
    {
        "id": "123",
        "recall_info": {
            "code":"ZGE",
            "description": "test1",
            "type": "AVL",
            "date": "09/08/2012"
        }
    },
    {
        "id": "123",
        "recall_info": {
            "code": "ZDN",
            "description": "test2",
            "type": "CLR",
            "date": "16/02/2012"
        }
    }
]';

$php = array(
    (object) array(
        'id' => '123',
        'recall_info' => (object) array(
            'code' => 'ZGE',
            'description' => 'test1',
            'type' => 'AVL',
            'date' => '09/08/2012'
        )
    ),
    (object) array(
        'id' => '123',
        'recall_info' => (object) array(
            'code' => 'ZGE',
            'description' => 'test2',
            'type' => 'CLR',
            'date' => '16/02/2012'
        )
    )
);

var_dump(json_encode($php));

the JSON format you've specified in the first example (ie the requested format) is not valid JSON.

A valid JSON string must evaluate to a single Javascript object; the example you've given evaluates to two Javascript objects, separated by a comma. In order to make it valid, you would need to either enclose the whole thing in square brackets, to turn it into a JS array or enclose it in curly braces, and give each of the two objects a key.

The PHP code you've written is doing the second of these two options. It is therefore generating valid JSON code, about as close to the original request as could be expected while still being valid.

It would help if you'd shown us the PHP code that you've used to do this; without that, I can't really give you advice on how to improve it, but if you want to switch to the square bracket notation, all you need is to put your PHP objects into an unkeyed array, and json_encode() should do it all for you; you shouldn't need to use a keyed array or an iterator for that.