将关联数组中的项添加到数组的每个关联数组

时间:2015-08-06 18:31:59

标签: php arrays ajax json codeigniter

我正在尝试合并来自2个API调用的数据。第一次调用获取我需要的大部分数据,第二次调用使用第一次调用的项(bill_id)作为参数,以获取第二次API调用的数据。我只需要第二个调用中的一个项目,所以我试图将它添加到第一个数组,然后将该数组作为json传递给客户端。

这是webmethod控制器..

    //AJAX calls
    function getUpcomingBills ($params = null) {
        $upcomingBills = $this->CongressAPI->get('upcoming_bills', $params);
        $upcomingBills = json_decode($upcomingBills, true);


        foreach($upcomingBills as $bill) {
            $p = ['bill_id' => $bill['bill_id']];
            $title = $this->CongressAPI->get('bills', $p);

            $bill['title'] = $title[17];
        }

        echo json_encode($upcomingBills);
    }

模特......

    public function get($method = null, $params = null) {

        $key = "...";
        $url = "http://congress.api.sunlightfoundation.com/";
        $params['apikey'] = $key;

        $ret = $this->curl->simple_get($url.$method, $params);

        if (!empty($ret)) {
            $ret = json_decode($ret, true);
            $ret = json_encode($ret["results"]);                
        } else {
            $ret = '';
        }

        return $ret;
    }

这会返回一个没有错误的JSON对象,但是它不会将“Title”附加到第一个Object。

另外fyi,当我尝试使用第二个调用的关联数组的标题(而不是$ title [17])时,我得到错误:

  

消息:非法字符串偏移'short_title'

第二次API调用返回以下内容:(试图获取'short_title')

{
    "results": [
        {
            "bill_id": "s754-114",
            "bill_type": "s",
            "chamber": "senate",
            "committee_ids": [
                "SLIN"
            ],
            "congress": 114,
            "cosponsors_count": 0,
            "enacted_as": null,
            "history": {
                "active": true,
                "active_at": "2015-04-15",
                "awaiting_signature": false,
                "enacted": false,
                "vetoed": false
            },
            "introduced_on": "2015-03-17",
            "last_action_at": "2015-08-05",
            "last_version": {
                "version_code": "pcs",
                "issued_on": "2015-03-17",
                "version_name": "Placed on Calendar Senate",
                "bill_version_id": "s754-114-pcs",
                "urls": {
                    "html": "http://www.gpo.gov/fdsys/pkg/BILLS-114s754pcs/html/BILLS-114s754pcs.htm",
                    "pdf": "http://www.gpo.gov/fdsys/pkg/BILLS-114s754pcs/pdf/BILLS-114s754pcs.pdf",
                    "xml": "http://www.gpo.gov/fdsys/pkg/BILLS-114s754pcs/xml/BILLS-114s754pcs.xml"
                },
                "pages": 54
            },
            "last_version_on": "2015-03-17",
            "last_vote_at": null,
            "number": 754,
            "official_title": "An original bill to improve cybersecurity in the United States through enhanced sharing of information about cybersecurity threats, and for other purposes.",
            "popular_title": null,
            "related_bill_ids": [
                "hr1560-114",
                "hr1731-114"
            ],
            "short_title": "Cybersecurity Information Sharing Act of 2015",
            "sponsor": {
                "first_name": "Richard",
                "last_name": "Burr",
                "middle_name": "M.",
                "name_suffix": null,
                "nickname": null,
                "title": "Sen"
            },
            "sponsor_id": "B001135",
            "urls": {
                "congress": "http://beta.congress.gov/bill/114th/senate-bill/754",
                "govtrack": "https://www.govtrack.us/congress/bills/114/s754",
                "opencongress": "https://www.opencongress.org/bill/s754-114"
            },
            "withdrawn_cosponsors_count": 0
        }
    ],
    "count": 1,
    "page": {
        "count": 1,
        "per_page": 20,
        "page": 1
    }
}

如果有任何不清楚或者您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

感谢GreeKatrina的参考,我通过将我的方法更改为以下方法来实现它。

function getUpcomingBills ($params = null) {
            $upcomingBills = $this->CongressAPI->get('upcoming_bills', $params);
            $upcomingBills = json_decode($upcomingBills, true);


            foreach($upcomingBills as $bill => $val) {
                $p = ['bill_id' => $upcomingBills[$bill]['bill_id']];
                $title = json_decode($this->CongressAPI->get('bills', $p), true);
                $upcomingBills[$bill]['title'] = $title;
            }

            echo json_encode($upcomingBills);
        }

它现在传递对象中的who json数组