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

我正在使用 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

        )

)

我的代码


数据库/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

            ];

        }


    }

任何帮助将不胜感激。


梦里花落0921
浏览 137回答 2
2回答

蝴蝶不菲

这应该可以帮助您实现目标。它涉及正则表达式的一些高级用法。//Load in meta data$self_developments = $candidate->getAllSelfDevelopments();//Create container for results$results = [];foreach($meta_values as $meta) {&nbsp; &nbsp; //Create Named matching groups that go into $matches. See PHP docs for details.&nbsp; &nbsp; preg_match('/(?P<key>.+)_(?P<index>\d+)/',$meta['meta_key'], $matches);&nbsp; &nbsp; //Use our index value to act as a guide to place our data. If the index does not exist, make it an empty array.&nbsp; &nbsp; //Otherwise, merge in our existing data with the new data.&nbsp; &nbsp; $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(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [qualification_0] => Karate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [qualification_type_0] => Course&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [qualification_2] => Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [qualification_type_2] => Certificate&nbsp; &nbsp; &nbsp; &nbsp; ))或者,如果您选择重新编制索引:Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [qualification_0] => Karate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [qualification_type_0] => Course&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [qualification_2] => Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [qualification_type_2] => Certificate&nbsp; &nbsp; &nbsp; &nbsp; ))

狐的传说

这个解决方案可以解决吗?$array = [&nbsp; &nbsp; 'qualification_0' => 'Karate',&nbsp; &nbsp; 'qualification_2' => 'Test',&nbsp; &nbsp; 'qualification_type_0' => 'Course',&nbsp; &nbsp; 'qualification_type_2' => 'Certificate',];foreach ($array as $key => $value) {&nbsp; &nbsp; preg_match('~(.+)_(\d+)~', $key, $match);&nbsp; &nbsp; ${$match[1]}[$match[2]] = $value;}$result = array_combine($qualification, $qualification_type);print_r($result);它会输出:Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; [Karate] => Course&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; [Test] => Certificate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP