简体   繁体   中英

PHP CodeIgniter Batch Insert Not Accepting My Array

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);

Here's the errors:

A PHP Error was encountered

Severity: Warning

Message: array_keys() [function.array-keys]: The first argument should be an array

Filename: database/DB_active_rec.php

Line Number: 1148

A PHP Error was encountered

Severity: Warning

Message: sort() expects parameter 1 to be array, null given

Filename: database/DB_active_rec.php

Line Number: 1149

Any ideas? Thanks!

Looks like you need to wrap your array in a second array. You're supposed to provide an array of row arrays.

$insert_array = array(array(...));

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM