0

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!

2 Answers 2

3

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(...));
Sign up to request clarification or add additional context in comments.

1 Comment

How can I create such an array from a normal array like Array( [0] => Array ( [track_id] => [camp_id] => 1 [email_title] => sample ) [1] => Array ( [track_id] => [camp_id] => 1 [email_date] => 2013-07-02 ) [2] => Array ( [track_id] => [camp_id] => 1 [email_template] => 2 )
2

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.

3 Comments

I think it may be because my array is too large. It's working, but can insert batch do 1000+ records at a time?
I don't think it would be too many records, as you would get a different error. This part of your code looks suspicious: (!empty($a['ContactPhoneAreaCode1']) ? $a['ContactPhoneAreaCode1'] : 'DEFAULT'). I think this code returns a boolean because you have it wrapped in parentheses. Although i don't see this as the culprit, I agree with @HerrKaleun and that you might check the elements of the array, as all of the other code looks correct (based on CIs docs).
I agree with swatkins too, you should check every array one by one because the 'basic edition' of your code works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.