如何组合两个Get_User()数组?

时间:2021-05-13 作者:Ashif

q1美元和q2美元各自运行良好。

if (!empty($search_term)) {

        $q1 = get_users(array(
            \'fields\' => \'all\',
            \'role\' => \'contact\',
            \'search\' => \'*\'.esc_attr( $search_term ).\'*\',
        ));

        $q2 = get_users(array(
            \'fields\' => \'all\',
            \'role\' => \'contact\',
            \'meta_query\' => array(
                \'relation\' => \'OR\',
                array(
                    \'key\'     => \'first_name\',
                    \'value\'   => $search_term,
                    \'compare\' => \'LIKE\'
                ),
                array(
                    \'key\'     => \'last_name\',
                    \'value\'   => $search_term,
                    \'compare\' => \'LIKE\'
                ),
                array(
                    \'key\' => \'location\',
                    \'value\' => $search_term ,
                    \'compare\' => \'LIKE\'
                )
            )
        ));

        $all_contacts = array_merge( $q1, $q2 );
        //$all_contacts = $q1;

    }

    $contacts_array = array();

    if (count($all_contacts) > 0 ){
        
        foreach ($all_contacts as $index => $contact) {
            $contacts_array[] = array(
                "id" => $contact->ID,
                "name" => $contact->display_name,
                "email" => $contact->user_email,
                "phone" => $contact->phone_number,
                "country" => $contact->location,
                "website" => $contact->user_url,
            );
        }
    }

    return $contacts_array;
如何将它们合并在一起以获得结果?

1 个回复
最合适的回答,由SO网友:phatskat 整理而成

Edit: 为了澄清,答案是

如何将它们合并在一起以获得结果?

如果没有大量自定义SQL和使用过滤器,您真的无法做到这一点。走这条路的复杂性远远超过了任何可感知的好处,也不是很好的未来证明。我建议使用下面的解决方案,并将其全部放入一个方法中,您可以调用该方法来获取所需的内容。

Original Answer

我在本地测试了这一点,并能够得到两组结果-从那里,我通过回调将它们传递给array_filter 要删除重复条目,请执行以下操作:

$q1 = get_users(array(
    \'fields\' => \'all\',
    \'role\'   => \'contact\',
    \'search\' => \'*\'.esc_attr( $search_term ).\'*\',
));

$q2 = get_users(array(
    \'fields\' => \'all\',
    \'role\'   => \'contact\',
    \'meta_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'key\'     => \'first_name\',
            \'value\'   => $search_term,
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\'     => \'last_name\',
            \'value\'   => $search_term,
            \'compare\' => \'LIKE\'
        ),
        array(
            \'key\' => \'location\',
            \'value\' => $search_term ,
            \'compare\' => \'LIKE\'
        )
    )
));

$results = array_merge( $q1, $q2 );
$results = array_filter( $results, function( $user ) {
    static $found_users = [];

    if ( in_array( $user->ID, $found_users, true ) ) {
        return false;
    }

    $found_users[] = $user->ID;
    return true;
} );
这个static 数组在每次调用闭包时都被持久化,允许代码跟踪哪些用户已经被处理过。如果用户ID位于$found_users 阵列,我们return false 从数组中删除当前元素。

相关推荐

nothing happen in search form

我想创建搜索表单,但当我搜索时什么都没有发生,这是代码:索引。php: <div class=\"tech-btm\"> <?php get_search_form();?> </div> 搜索表单:<form role=\"search\" method=\"get\" id=\"searchform\" action=\"<?php echo home_url(\'/\')?>\"> &