json里面的组元素

时间:2014-03-06 11:40:45

标签: php json

我正在尝试制作一个看起来像这样的json:

{
    "data": {
        "error_message": "",
        "data": {
            "person": [
                [
                    {
                        "name": "teset1",
                        "image": "http://test.co.il/test/icons/billadress.png"
                    },
                    {
                        "name": "test2",
                        "image": "http://test.co.il/test/icons/email.png"
                    },
                    {
                        "name": "test3",
                        "image": "http://test.co.il/test/icons/homeicon.png"
                    }
                ],
                [
                    {
                        "name": "a",
                        "image": "http://test.co.il/shay/keykit/icons/billadress.png"
                    },
                    {
                        "name": "b",
                        "image": "http://test.co.il/shay/keykit/icons/email.png"
                    },
                    {
                        "name": "c",
                        "image": "http://dvns.co.il/shay/keykit/icons/homeicon.png"
                    }
                ],
                [
                    {
                        "name": "a",
                        "image": "http://test.co.il/test/icons/billadress.png"
                    },
                    {
                        "name": "b",
                        "image": "http://test.co.il/test/icons/email.png"
                    },
                    {
                        "name": "c",
                        "image": "http://dvns.co.il/test/icons/homeicon.png"
                    }
                ],

            ]
        }
    },

}

这是我在获得名称后使用的代码&来自mysql查询的图片:

$response = $this->_db->query($query)->result_array();
$icons_results = array();

$obj = new stdclass();
foreach ($response as $key => $response) {
    $icons_model = new icons_model();

    $icons_model->init($response);
    $obj->families[] = $icons_model;


}

return $obj;  

所以......这就是我得到的:

{
    "data": {
        "families": [
            {
                "id": "1",
                "name": "a",
                "image_url": "http://test.co.il/shay/keykit/icons/billadress.png"
            },
            {
                "id": "2",
                "name": "b",
                "image_url": "http://test.co.il/shay/keykit/icons/email.png"
            },
            {
                "id": "3",
                "name": "c",
                "image_url": "http://test.co.il/test/icons/homeicon.png"
            },
            {
                "id": "6",
                "name": "f",
                "image_url": "http://test.co.il/test/icons/homeicon.png"
            },
            {
                "id": "4",
                "name": "d",
                "image_url": "http://test.co.il/test/icons/billadress.png"
            },
            {
                "id": "5",
                "name": "e",
                "image_url": "http://test.co.il/test/icons/billadres2323s.png"
            },

并继续。

如何让每三个对象都在一个组中? (比如第一个例子)

1 个答案:

答案 0 :(得分:1)

试试这个:

foreach ($response as $key => $response) {
    $icons_model = new icons_model();

    $icons_model->init($response);
    $obj->families[(int)($key / 3)][] = $icons_model;
}