编程语言
首页 > 编程语言> > php-在私有函数的数组上插入元素

php-在私有函数的数组上插入元素

作者:互联网

如何将元素插入该数组位于私有函数的数组的最终位置?

private function getData()
{

    return array(
            1 => array(
                    'open_cookie_id' => 1,
                    'text' => 'I may throw up on ya',
                    'who'  => 'Leonard McCoy',
            ),
            2 => array(
                    'open_cookie_id' => 2,
                    'text' => 'I think these things are pretty safe.',
                    'who' => 'James T. Kirk'
            ),
            3 => array(
                    'open_cookie_id' => 3,
                    'text' => 'Well, I hate to break this to you, but Starfleet operates in space.',
                    'who' => 'James T. Kirk'
            ),
            4 => array(
                    'open_cookie_id' => 4,
                    'text' => 'Yeah. Well, I got nowhere else to go. The ex-wife took the whole damn planet in the divorce. All I got left is my bones.',
                    'who' => 'Leonard McCoy'
            ),
            5 => array(
                    'open_cookie_id' => 5,
                    'text' => 'If you eliminate the impossible, whatever remains, however improbable, must be the truth.',
                    'who' => 'Spock'
            )
    );

}

如何将元素6、7、8等插入到这些函数的最终数组中,而不是其他函数
从此功能:

  /**
     * Create a resource
     *
     * @param  mixed $data
     * @return ApiProblem|mixed
     */
    public function create($data)
    {
        //return new ApiProblem(405, 'The POST method has not been defined');
        //return $this->create($data) ;
        $adapter = new ArrayAdapter($this->getData());
        $adapter2 = Array
        (
            $data->open_cookie_id => array(
                                    'open_cookie_id'=>$data->open_cookie_id ,
                                    'text' =>$data->text, 
                                    'who' => $data->who
                                        )
        );

        $adapter2_converted = new ArrayAdapter($adapter2);
        //operation for merge two ArayAdapter ($adapter+$adapter2_convert)
       // $collection = new OpenCookieCollection($final_adapter);
        //return $collection;
    }

我正在使用php zend框架和功能.

解决方法:

该函数是私有的而不是数组,因此您可以安全地使用返回的数组.请注意,$adapter是ArrayAdapter数据类型,而不是简单的数组,因此您不能简单地推送.

我的建议是向您的ArrayAdapter添加一个方法,该方法使用PHP array_push()将数组添加到ArrayAdapter数据结构中,并使用如下方法:
$adapter-> pushArray($adapter2);

标签:zend-framework2,php,apigility
来源: https://codeday.me/bug/20191027/1944945.html