PHP CodeIgniter批处理插入不接受我的数组 [英] PHP CodeIgniter Batch Insert Not Accepting My Array
问题描述
我无法使以下代码正常工作,这与数组的形成有关。该数组实际上是在foreach()循环运行几次之后构建的,然后我想批量插入,但是格式错误。为什么?
I'm unable to get the following code to work, and it has something to do with the forming of the array. The array is actually build after a foreach() loop runs a few times, then I want to batch insert, but it comes up malformed. Why?
foreach ($results as $r) {
$insert_array = array(
'ListingRid' => $r['ListingRid'],
'StreetNumber' => $r['StreetNumber'],
'StreetName' => $r['StreetName'],
'City' => $r['City'],
'State' => $r['State'],
'ZipCode' => $r['ZipCode'],
'PropertyType' => $r['PropertyType'],
'Bedrooms' => $r['Bedrooms'],
'Bathrooms' => $r['Bathrooms'],
'YearBuilt' => (2011 - $r['Age']),
'Status' => $r['Status'],
'PictureCount' => $r['PictureCount'],
'SchoolDistrict' => $r['SchoolDistrict'],
'ListedSince' => $r['EntryDate'],
'MarketingRemarks' => $r['MarketingRemarks'],
'PhotoUrl' => (!empty($photo_url) ? $photo_url : 'DEFAULT'),
'ListingAgentFirstName' => $r['ListingAgentFirstName'],
'ListingAgentLastName' => $r['ListingAgentLastName'],
'ContactPhoneAreaCode1' => (!empty($a['ContactPhoneAreaCode1']) ? $a['ContactPhoneAreaCode1'] : 'DEFAULT'),
'ContactPhoneNumber1' => (!empty($a['ContactPhoneNumber1']) ? $a['ContactPhoneNumber1'] : 'DEFAULT'),
'ListingOfficeName' => (!empty($r['ListingOfficeName']) ? $r['ListingOfficeName'] : 'DEFAULT'),
'OfficePhoneComplete' => (!empty($o['OfficePhoneComplete']) ? $o['OfficePhoneComplete'] : 'DEFAULT'),
'last_updated' => date('Y-m-d H:i:s')
);
$insert[] = $insert_array;
}
$this->db->insert_batch('listings', $insert);
这是错误:
遇到PHP错误
A PHP Error was encountered
严重性:警告
消息:array_keys()[函数.array-keys]:第一个参数应该
是一个数组
Message: array_keys() [function.array-keys]: The first argument should be an array
文件名:database / DB_active_rec.php
Filename: database/DB_active_rec.php
行号:1148
遇到PHP错误
严重性:警告
消息:sort()期望参数1为数组,给定为空
Message: sort() expects parameter 1 to be array, null given
文件名:database / DB_active_rec.php
Filename: database/DB_active_rec.php
行号:1149
有什么想法吗?谢谢!
推荐答案
我已将您的代码减少到最低限度,并且似乎可以正常工作。
I've reduced your code to the bare minimum and it seems to work.
$i = 0;
while ($i <= 10) {
$insert_array = array(
'code' => 'asd'
);
$insert[] = $insert_array;
$i++;
}
$this->db->insert_batch('group', $insert);
您应该检查数组的元素,将其全部注释并逐一注释,直到你是罪魁祸首。
You should check the elements of the array, comment them all and de-comment them one by one until you got the culprit.
这篇关于PHP CodeIgniter批处理插入不接受我的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!