如何将元素插入数组的最终位置?这个数组在私有函数上??
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框架和apigility。
function
是私有而不是array
,因此您可以安全地使用返回的array
。请注意,$adapter
是ArrayAdapter
数据类型,而不是简单的array
,因此您不能简单地推送。
我的建议是在ArrayAdapter
中添加一个方法,该方法使用PHP array_push()
将数组添加到ArrayAdapter
数据结构中,并如下使用:$adapter->pushArray($adapter2);
我认为这是您实际调用私有方法getData()
:的行
$adapter = new ArrayAdapter($this->getData());
如果你所需要的只是一个添加了一些额外元素的数组,你可以这样做:
$data = $this->getData();
$data[] = 'more data';
$data[] = 'even more data';
$adapter = new ArrayAdapter($data);