目前我有一个脚本将从数组中收集数据并将其推送到API,但由于某些原因,我无法一次添加多个数据。
$ data Array
array
(
[0] => Array
(
[_id] => 0
[_source] => App
[_source_id] => 790006230e91dd6279a42a814bf5965a.jpg
[_client_ids] => 2
[_parent_ids] => 0
[_image] => blank.jpg
[_title] => test1
[_body] => test1
)
[1] => Array
(
[_id] => 1
[_source] => App
[_source_id] => b3a5a6df0cf1dfc30882b20f8a493092.jpg
[_client_ids] => 2
[_parent_ids] => 0
[_image] => blank.jpg
[_title] => test2
[_body] => test2
)
)
PHP
public function save($data) {
$submissionManager = new SubmissionManager($this->container);
$returndata = array();
foreach( $data as $entry )
{
$this->mustBeGranted('PERM_SUBMISSION_CREATE');
$saved = array();
$client_ids = explode(',', $entry['_client_ids']);
$parent_ids = explode(',', $entry['_parent_ids']);
$length = count($client_ids);
for($i = 0; $i < $length; $i++){
$input = array_merge($entry, array(
'client_id' => isset($client_ids[$i]) ? $client_ids[$i] : 0,
'parent_id' => isset($parent_ids[$i]) ? $parent_ids[$i] : 0,
'_source_id' => $entry['_source_id']
));
$saved[] = $submissionManager->createSubmission($input);
}
$returndata[ $entry['_id']] = $saved;
}
}
基本上它只会将第一个数组推送到API而不是第二个数组,即使我已经添加了for-each。
答案 0 :(得分:1)
从foreach循环中删除$ saved = array()并将其放在循环之前。每次循环开始时都要分配空数组
答案 1 :(得分:0)
我想说$entry['_id']
的值可能没有改变,所以你要重新分配到同一个数组键。
尝试使用简单:
$returndata[] = $saved;
答案 2 :(得分:0)
抱歉修复了与我的createSubmission类有关的问题...我的错误