简体   繁体   中英

Large arrays of objects not referencable by keys

We send out emails to users with the latest jobs, so we have a array of about 1800 user objects.

I use a for loop to iterate through the objects

$part = $this->getRequest()->getParam('part') ? : 1;
$parts = 2;

$jobagents = RAD::registry('jobagents');
$jobagentsLength = count($jobagents);   
$sliceCount = $jobagentsLength / $parts;

for ($x = 0; $x < $jobagentsLength; $x++) {
    $slice = $x + 1;
    if ($slice > ceil($sliceCount * ($part - 1)) && $slice <= ceil($sliceCount * $part)) {
        $jobagent = $jobagents[$x];
    }

}

The slice if is used to determain which part of the object array we use to send out (it's to heavy to send out in one chuck) - I release this could probably be done smarter - But the if passes so that isn't the issue at hand.

The issue is

 $jobagent = $jobagents[$x];

The first loop is fine, $jobagent is now a object - but after that it appears empty, and i've checked that i looped through everything by adding a echo to the loop - And i've tried print_r'ing the array, and it has 1800+ objects with keys from 0-18xx

Am i missing something with arrays of objects above a certain size and referencing them by keys? If i use a foreach the objects are fine.

Issue resolved, array was overwritten in edge case

For sure, that you have really plain array

$jobagents = array_values(RAD::registry('jobagents'));

If you really receive a list of objects (= it's a proper array), then for simplicity try:

$jobagents = array_slice($jobagents, $i * $sliceCount, $sliceCount);
foreach ($jobagents as $j) {

However I suspect that's not your actual problem source. Objects that unset themselves are somewhat uncommon.

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