dqmg80654 2014-08-03 22:32
浏览 36

循环遍历数组并对每个结果执行函数[关闭]

I am trying to pull the [title] out of the following array and then perform a function with each title.

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

Here is the php I am using to try and take the group title and then pass each of them through an API for an SMS service to add the create each group on the 3rd party site and then add the contact to that group.

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

Here is the full code:

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条回答 默认 最新

  • doushan1863 2014-08-03 22:39
    关注

    Assuming that $array is your whole containing array:

    for($i=0;$i<count($array);$i++){
    
    foreach($array[$i] as $key => $value){
    
      if($key == 'title'){
                    $group = $project->getOrCreateGroup($value);
                    $contact->addToGroup($group); 
       }
    
     }
    
    }
    

    If this doesn't help you, please narrow down your request.

    Good Luck

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题