简体   繁体   English

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

[英]Add an item from an associative array to each associative array of an array

I'm trying to combine data from 2 API calls. 我正在尝试合并2个API调用中的数据。 The first call gets most the data I need, the second call that's made uses an item(bill_id) from the first call as a parameter in order to get data for the second API call. 第一个调用获取我所需的大部分数据,第二个调用使用第一个调用中的item(bill_id)作为参数,以获取第二个API调用的数据。 I only need 1 item from the second call so I'm trying to add it to the first array and then pass that array as json to the client side. 我只需要第二个调用中的一项,所以我试图将其添加到第一个数组中,然后将该数组作为json传递给客户端。

Here's the webmethod controller.. 这是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);
    }

And the model... 还有那个模特

    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;
    }

This is returning a JSON object without error, however it's not appending the "Title" to the first Object. 这将返回没有错误的JSON对象,但是不会将“标题”附加到第一个对象。

Also fyi, when I try and use the title of the associative array of the second call (instead of $title[17]), I get the error: 同样,当我尝试使用第二个调用的关联数组的标题(而不是$ title [17])时,出现错误:

Message: Illegal string offset 'short_title' 消息:非法字符串偏移量“ short_title”

2nd API call returns the following: (Trying to get '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
    }
}

Please let me know if there's anything unclear or if you need more info. 如果有任何不清楚的地方或需要更多信息,请告诉我。

Thanks to the reference by GreeKatrina, I got it working by changing my method to the following. 感谢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);
        }

It now passes the who json array within the object 现在它将在对象内传递who json数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM