循环遍历数组并对每个结果执行函数

时间:2014-08-03 22:32:59

标签: php loops foreach civicrm

我试图将[title]拉出以下数组,然后对每个标题执行一个函数。

阵列:

Array ( 
    [is_error] => 0 
    [version] => 3 
    [count] => 2
    [values] => Array ( 
        [5] => Array ( 
            [id] => 5 
            [group_id] => 1 
            [title] => 2014rootedemail 
            [visibility] => User and User Admin Only 
            [is_hidden] => 0 
            [in_date] => 2014-08-01 07:45:39 
            [in_method] => Admin
        )
        [6] => Array ( 
            [id] => 6 
            [group_id] => 2 
            [title] => Student 2014 
            [visibility] => User and User Admin Only 
            [is_hidden] => 0 
            [in_date] => 2014-08-01 08:23:22 
            [in_method] => Admin
        )
    ) 
)

这是我用来尝试获取组标题的php,然后通过一个用于SMS服务的API传递它们,以便在第三方网站上添加创建每个组,然后将联系人添加到该组。

foreach ($result as &$groupt){
    $group = $project->getOrCreateGroup($groupt['title']);
    $contact->addToGroup($group); 
}

以下是完整代码:

defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

class plgCiviCRMBase extends JPlugin
{

    /**
     * Base Civicrm Plugin.
     *
     * @package     Civicrm
     * @subpackage  Joomla plugins
     * @since       1.6
     */

    function civicrm_post( $op, $objectName, $objectId, &$objectRef ) 
    {

        if (($op == 'create' || $op =='edit') && $objectName == 'Individual') {

            //Telerivet API CONNECT
            require_once JPATH_SITE.'/media/civicrm/telerivet/telerivet.php';
            $API_KEY = '----';
            $PROJECT_ID = '----';
            $telerivet = new Telerivet_API($API_KEY);
            $project = $telerivet->initProjectById($PROJECT_ID);


            //Get Variables Civicrm
            $firstName = $objectRef->first_name;
            $lastName = $objectRef->last_name;
            $phone = $objectRef->phone[0]->phone;
            $displayName = $objectRef->display_name;

            //Get Groups the contact is in... Returns Above array
            $params = array( 'contact_id' => $objectRef->id,'version' => 3,);
            require_once 'api/api.php';
            $result = civicrm_api( 'group_contact','get',$params );

            // Telerivet Post New Contact to Telerivet 
            $contact = $project->getOrCreateContact(array(
                'name' => $displayName, 
                'phone_number' => $phone, 
                'vars' => array('first' => $firstName, 'last' => $lastName)
            ));


            // Add Contact to Telerivet Groups
            //? Now how do I get each of the groups and then add the contact below for each group. for the contact?

            foreach ($result as &$groupt){
                $group = $project->getOrCreateGroup($groupt['title']);
                $contact->addToGroup($group); 
            }

            //print_r($groupt);
            //exit();
         }
    }
}

2 个答案:

答案 0 :(得分:1)

As Charlotte Dunois pointed out in the comments


您需要遍历values数组。现在你的数组看起来像这样:

$a = array(
    array(
        'is_error' => 0,
        'version' => 3,
        'count' => 2,
        'values' => array(
            '5' => array(
                'id' => 5,
                'group_id' => 1,
                'title' => '2014rootedemail',
                'visibility' => 'User and User Admin Only',
                'is_hidden' => 0,
                'in_date' => '2014-08-01 07:45:39',
                'in_method' => 'Admin',
            ),
            '6' => array(
                'id' => 6,
                'group_id' => 2,
                'title' => 'Student 2014',
                'visibility' => 'User and User Admin Only',
                'is_hidden' => 0,
                'in_date' => '2014-08-01 08:23:22 ',
                'in_method' => 'Admin',
            )
        )
    )
);

你所要做的就是进入那个values数组,你就会笑。使用像这样的foreach循环足够简单:

foreach ($a as $i => $item) {
    foreach ($item['values'] as $i) {
        $group = $project->getOrCreateGroup($groupt['title']);
        $contact->addToGroup($group);
    }
}

Example

答案 1 :(得分:0)

假设$array是你的整个数组:

for($i=0;$i<count($array);$i++){

foreach($array[$i] as $key => $value){

  if($key == 'title'){
                $group = $project->getOrCreateGroup($value);
                $contact->addToGroup($group); 
   }

 }

}

如果这对您没有帮助,请缩小您的要求。

祝你好运