php通过匹配数组键的最后一个字符来合并数组

pu3pd22g  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(270)

我使用sql检索表单数据并用php显示它。字段显示为 qualification_0 以及 qualification_type_0 ,唯一改变的值是数字,所以我想将这些值分组到一个数组中,然后显示到正确的输入中。
用户可以删除输入,这样数字就不会总是增加1,而是成对发送。
目前
数据显示为:

Array
(
    [qualification_0] => Karate
    [qualification_2] => Test
    [qualification_type_0] => Course
    [qualification_type_2] => Certificate
)

目标
我正在努力实现以下目标:

Array
(
    [0] => Array
        (
            [qualification_0] => Karate
            [qualification_type_0] => Course
        )
    [1] => Array
        (
            [qualification_2] => Test
            [qualification_type_2] => Certificate
        )
)

我的代码
sql/php语言:

public function getAllSelfDevelopments()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . "usermeta";
        $result = $wpdb->get_results(
            "
            SELECT meta_key, meta_value
            FROM $table_name
            WHERE user_id = $this->user_id
            AND meta_key LIKE 'qualification%'
            ", ARRAY_A
        );

        return $result;
    }

查询结果:

Array
(
    [0] => Array
        (
            [meta_key] => qualification_0
            [meta_value] => Karate
        )

    [1] => Array
        (
            [meta_key] => qualification_2
            [meta_value] => Test
        )

    [2] => Array
        (
            [meta_key] => qualification_type_0
            [meta_value] => Course
        )

    [3] => Array
        (
            [meta_key] => qualification_type_2
            [meta_value] => Certificate
        )

)

尝试:

$self_developments = $candidate->getAllSelfDevelopments();

    $self_developments_arr = [];
    foreach($self_developments as $value){
        $self_developments_arr[$value['meta_key']] = $value['meta_value'];
    }

    $self_dev = [];
    foreach($self_developments_arr as $key => $value){

        if(strpos($key, $key[-1]) !== FALSE && !in_array($key, $self_dev)){
            $self_dev[] = [
                $key => $value
            ];
        }

    }

任何帮助都将不胜感激。

utugiqy6

utugiqy61#

这会让你达到你的目标。它涉及到正则表达式的一些高级用法。

//Load in meta data
$self_developments = $candidate->getAllSelfDevelopments();

//Create container for results
$results = [];
foreach($meta_values as $meta) {

    //Create Named matching groups that go into $matches. See PHP docs for details.
    preg_match('/(?P<key>.+)_(?P<index>\d+)/',$meta['meta_key'], $matches);

    //Use our index value to act as a guide to place our data. If the index does not exist, make it an empty array.
    //Otherwise, merge in our existing data with the new data.
    $results[$matches['index']] = array_merge($results[$matches['index']] ?? [], [$meta['meta_key'] => $meta['meta_value']]);

}
//if you want to re-index the array.
$results = array_values($results);

应输出:

Array
(
    [0] => Array
        (
            [qualification_0] => Karate
            [qualification_type_0] => Course
        )

    [2] => Array
        (
            [qualification_2] => Test
            [qualification_type_2] => Certificate
        )

)

或者,如果您选择重新编制索引:

Array
(
    [0] => Array
        (
            [qualification_0] => Karate
            [qualification_type_0] => Course
        )

    [1] => Array
        (
            [qualification_2] => Test
            [qualification_type_2] => Certificate
        )

)
c7rzv4ha

c7rzv4ha2#

这个解决方案能解决吗?

$array = [
    'qualification_0' => 'Karate',
    'qualification_2' => 'Test',
    'qualification_type_0' => 'Course',
    'qualification_type_2' => 'Certificate',
];

foreach ($array as $key => $value) {
    preg_match('~(.+)_(\d+)~', $key, $match);
    ${$match[1]}[$match[2]] = $value;
}

$result = array_combine($qualification, $qualification_type);
print_r($result);

它将输出:

Array                                                                                                                                                     
(                                                                                                                                                         
    [Karate] => Course                                                                                                                                    
    [Test] => Certificate                                                                                                                                 
)

相关问题