我正在测试数组的内容$list
,当我循环遍历这些递归函数调用时,我将返回。
一次有25个记录的硬限制。
在结果集等于限制的调用中,我增加一个偏移量并递归发送当前结果数组作为参数。
从这些循环中,我将$ list var和其他详细信息转储到一封电子邮件中,指示是否递归或返回以及当前计数和$ list数组的转储。
这些电子邮件完全包含我期望的数字和$list
。
然而,调用脚本似乎只有一个不同的$list
只包含最后一个循环。如果结果集超过25 $list
为空。如果整个集合小于25,则在调用脚本输出中是正确的。
有人可以直截了当地说明为什么$list
在函数内部看起来不同于它返回后的原因?
调用脚本:
$levels = get_membership_levels();
if ($levels) {
foreach ($levels as $l) {
echo "<h2>$l</h2>\n";
$members = get_current_members_by_level($l);
echo '<h2>' . count($members) . '</h2>';
功能:
function get_current_members_by_level($level, $offset = 0, $list = array()) {
require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/civicrm/civicrm.settings.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/civicrm/civicrm/CRM/Core/Config.php';
$config = CRM_Core_Config::singleton( );
require_once $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/civicrm/civicrm/api/api.php';
$result = civicrm_api3('Membership', 'get', array(
'membership_type_id' => "$level",
'limit' => 1000,
'offset' => $offset,
'api.Contact.get' => array(array('limit' => 1000, 'sort' => 'last_name')),
));
if ($result['values']) {
foreach ($result['values'] as $k => $v) {
$member = get_member($v['contact_id']);
if ($member) {
$list[]= $member;
}
}
if (count($result['values']) >= 25) {
$offset+= 25;
mail('email@example.com', "Recursing for $level - current set= " . count($result['values']) . ' Total: ' . count($list) . ' Offset= ' . $offset,print_r($list,1));
get_current_members_by_level($level, $offset, $list);
} else {
mail('email@example.com', "Returning result for $level - current set= " . count($result['values']) . ' Total: ' . count($list) . ' Offset= ' . $offset,print_r($list,1));
return $list;
}
}
}
答案 0 :(得分:0)
我认为您的代码中的问题是您不使用return或函数get_current_members_by_level();
if (count($result['values']) >= 25) {
$offset+= 25;
mail('email@example.com', "Recursing for $level - current set= " . count($result['values']) . ' Total: ' . count($list) . ' Offset= ' . $offset,print_r($list,1));
get_current_members_by_level($level, $offset, $list); // this function return the list but you dont use it.
} else {
mail('email@example.com', "Returning result for $level - current set= " . count($result['values']) . ' Total: ' . count($list) . ' Offset= ' . $offset,print_r($list,1));
return $list;
}
这应该是
if (count($result['values']) >= 25) {
$offset+= 25;
mail('email@example.com', "Recursing for $level - current set= " . count($result['values']) . ' Total: ' . count($list) . ' Offset= ' . $offset,print_r($list,1));
$list = get_current_members_by_level($level, $offset, $list); // now the new list will be in the var $list
} else {
mail('email@example.com', "Returning result for $level - current set= " . count($result['values']) . ' Total: ' . count($list) . ' Offset= ' . $offset,print_r($list,1));
return $list;
}