【问题标题】:PHP merge arrays by matching last character of array keyPHP通过匹配数组键的最后一个字符来合并数组
【发布时间】:2023-03-19 22:48:01
【问题描述】:

我正在使用 SQL 检索表单数据并使用 PHP 显示它。这些字段显示为qualification_0qualification_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
            ];
        }

    }

任何帮助将不胜感激。

【问题讨论】:

    标签: php sql arrays wordpress


    【解决方案1】:

    这应该可以帮助您实现目标。它涉及正则表达式的一些高级使用。

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

    【讨论】:

    • 非常感谢!!我只需要将 $meta_values 更改为 $self_developments。这比我的四个循环好多了!
    • 啊,我的错。我复制/粘贴了那行,忘记更新我的代码。
    【解决方案2】:

    这个解决方案可以解决吗?

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

    【讨论】:

    • 感谢您的回复!这段 PHP 代码看起来很高级,我不太确定 ${match} 行在做什么或 $result 如何检测不存在的变量。这接近我想要实现的目标,但请查看我的“目标”(第二个代码块)了解我想要实现的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    相关资源
    最近更新 更多