How can insert element into final position of array this array is on a private function??
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'
)
);
}
How to insert the element 6 , 7, 8 etc to final array on these function private from other function from this function:
/**
* 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;
}
I'm using php zend framework and apigility.
The function
is private not the array
so you can safely work with your returned array
. Do notice that $adapter
is a ArrayAdapter
data type, not a simple array
so you can't simple push.
My suggestion is to add a method to your ArrayAdapter
that uses PHP array_push()
to add your array to your ArrayAdapter
data structure and use like this: $adapter->pushArray($adapter2);
I think this is the line where you're actually calling the private method getData()
:
$adapter = new ArrayAdapter($this->getData());
If all you need as a result is an array with some extra elements added to it, you can do something like this:
$data = $this->getData();
$data[] = 'more data';
$data[] = 'even more data';
$adapter = new ArrayAdapter($data);