简体   繁体   English

试图创建一个关联数组 PHP

[英]Trying to create an associative array PHP

I have the following array.我有以下数组。 What I'm trying to do is get each element under "bill_ids", use the ID (eg "hjres61-114") to make another call and then retitle the 0 under "bill_ids" to the ID and then include another array under that element.我想要做的是获取“bill_ids”下的每个元素,使用 ID(例如“hjres61-114”)进行另一个调用,然后将“bill_ids”下的 0 重新命名为 ID,然后在该 ID 下包含另一个数组元素。

大批

Here's what I have and it's giving me this error..这是我所拥有的,它给了我这个错误..

Message: Illegal offset type消息:非法偏移类型

        $floor_updates = $this->congress->floor_updates($params); 

        foreach ($floor_updates as $update) {

            if ($update['bill_ids']) {

                foreach ($update['bill_ids'] as $bill => $bill_id) {
                    $billInfo = $this->bill->billSearch(['bill_id' => $bill_id]);

                    $floor_updates[$update]['bill_ids'][$bill][0] = $billInfo;
                }

            }
        }  

I'm terrible with php arrays and any guidance would be greatly appreciated..我对 php 数组很糟糕,任何指导将不胜感激..

What you actually want to do is the following:您真正想要做的是以下内容:

First, capture the array index of each of our update elements.首先,捕获每个更新元素的数组索引。 We can simply do that by passing in $array_index => $update .我们可以通过传入$array_index => $update来简单地做到这一点。

foreach ($floor_updates as $array_index => $update)

Now, we can access the $update array by $floor_updates[$array_index] .现在,我们可以通过$floor_updates[$array_index]访问$update数组。

$floor_updates[$array_index]['bill_ids'][$bill] = $billInfo;

In the above, there's no reason to access the 0th element of the array as the $bill actaully contains reference to the index of each key value pair, so we can simply just reference [$bill] to get access to the array.在上面,没有理由访问数组的0th元素,因为$bill实际上包含对每个键值对索引的引用,所以我们可以简单地引用[$bill]来访问数组。

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

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