【问题标题】:Add an item from an associative array to each associative array of an array将关联数组中的一项添加到数组的每个关联数组
【发布时间】:2015-10-30 01:05:46
【问题描述】:

我正在尝试合并来自 2 个 API 调用的数据。第一次调用获得了我需要的大部分数据,第二次调用使用第一次调用中的 item(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[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
    }
}

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

【问题讨论】:

标签: php arrays ajax json codeigniter


【解决方案1】:

感谢 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 数组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多