Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php GoogleAPI获取消息标签和嵌套标签_Php_Google Api Php Client - Fatal编程技术网

Php GoogleAPI获取消息标签和嵌套标签

Php GoogleAPI获取消息标签和嵌套标签,php,google-api-php-client,Php,Google Api Php Client,我的gmail帐户中有嵌套标签,例如(工作/编程)。 我是在PHP上开发的,我有这个显示标签列表的代码 $service_gmail = new Google_Service_Gmail( $client ); $lab = ( ! empty( $_GET['label'] ) ) ? $_GET['label'] : 'INBOX'; $labelsResponse = $service_gmail->users_labels->listUsersLabels('me'); $l

我的gmail帐户中有嵌套标签,例如(工作/编程)。 我是在PHP上开发的,我有这个显示标签列表的代码

$service_gmail = new Google_Service_Gmail( $client );
$lab = ( ! empty( $_GET['label'] ) ) ? $_GET['label'] : 'INBOX';
$labelsResponse = $service_gmail->users_labels->listUsersLabels('me');
$labels = array_merge($labels, $labelsResponse->getLabels());

<?php if ( ! empty( $labels ) ) { 
        foreach ( $labels as $key => $label ) :
        ?>
             <tr>
                <td width="1%"><span class="num"><?php echo ( $key + 1 ); ?></span></td>
                <td><h5><?php echo $label['name'];?></h5>
                    <small><?php echo $label['type'];?></small></td>
                <td><?php echo $service_gmail->users_labels->get( 'me', $label['id'] )->getMessagesUnread();?></td>
                <td><?php echo $service_gmail->users_labels->get( 'me', $label['id'] )->getMessagesTotal(); ?></td>
            </tr>
            <?php
        endforeach;
    }
?>

那么,有没有可能做到这一点,没有硬编码,所以我需要所有的是动态的

您将有两个数组$main\u标签和$sub\u标签。在foreach循环中,您将看到标签名称是否包含字符“/”。如果是,则将标签名称分解为“/”。然后,如果第一部分(在/之前)已经存在,您将在$main_标签中搜索。如果是,则将第二部分存储在$sub_标签中。最后,要回显标签,您将在$main_labels数组上循环,并在循环中查看$sub_labels[$main_lbl_key]是否为空。如果它不是空的,则存在子类别,您必须在$sub_标签[$main_lbl_key]上写入另一个循环以响应它们

例如:

<?php

$service_gmail = new Google_Service_Gmail( $client );
$lab = ( ! empty( $_GET['label'] ) ) ? $_GET['label'] : 'INBOX';
$labelsResponse = $service_gmail->users_labels->listUsersLabels('me');
$labels = array_merge($labels, $labelsResponse->getLabels());

$main_labels = $sub_labels = array();

function ifMainLabelExists($lbl) {
    global $main_labels;
    foreach ($main_labels as $k => $l) {
        if (strcmp($lbl['name'], $l) === 0) {
            return $k;
        }
    }

    return -1;
}

if ( ! empty( $labels ) ) { 
    foreach ( $labels as $key => $label ) {
        if (strpos($label['name'], '/') !== false) {
            $label_parts = explode('/', $label['name']); // break label name at  character '/'
            if (count($label_parts) > 1) {
                $m = ifMainLabelExists($label_parts[0]);
                if ($m !== -1) { // main label already exists
                    $label['name'] = $label_parts[1];
                    $sub_labels[$m][] = $label;
                } else { // main label does not exist already
                    $label['name'] = $label_parts[0];
                    $main_labels[] = $label;
                    $m = count($main_labels) - 1; // will be inserted at last index

                    $label['name'] = $label_parts[1];
                    $sub_labels[$m][] = $label;
                }
            }
        } else {
            $main_labels[] = $label;
        }
    }
}

// now echo them

<?php

$service_gmail = new Google_Service_Gmail( $client );
$lab = ( ! empty( $_GET['label'] ) ) ? $_GET['label'] : 'INBOX';
$labelsResponse = $service_gmail->users_labels->listUsersLabels('me');
$labels = array_merge($labels, $labelsResponse->getLabels());

$main_labels = $sub_labels = array();

function ifMainLabelExists($lbl) {
    global $main_labels;
    foreach ($main_labels as $k => $l) {
        if (strcmp($lbl['name'], $l) === 0) {
            return $k;
        }
    }

    return -1;
}

if ( ! empty( $labels ) ) { 
    foreach ( $labels as $key => $label ) {
        if (strpos($label['name'], '/') !== false) {
            $label_parts = explode('/', $label['name']); // break label name at  character '/'
            if (count($label_parts) > 1) {
                $m = ifMainLabelExists($label_parts[0]);
                if ($m !== -1) { // main label already exists
                    $label['name'] = $label_parts[1];
                    $sub_labels[$m][] = $label;
                } else { // main label does not exist already
                    $label['name'] = $label_parts[0];
                    $main_labels[] = $label;
                    $m = count($main_labels) - 1; // will be inserted at last index

                    $label['name'] = $label_parts[1];
                    $sub_labels[$m][] = $label;
                }
            }
        } else {
            $main_labels[] = $label;
        }
    }
}

// now echo them