亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP通過匹配數組鍵的最后一個字符來合并數組

PHP通過匹配數組鍵的最后一個字符來合并數組

PHP
夢里花落0921 2023-04-15 10:31:12
我正在使用 SQL 檢索表單數據并使用 PHP 顯示它。這些字段顯示為qualification_0和qualification_type_0,唯一改變的值是數字,所以我想將這些值分組到一個數組中,然后我可以將其顯示到正確的輸入中。用戶可以刪除輸入,因此數字不會總是遞增 1,但總是成對發送?,F在數據顯示為: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            ];        }    }任何幫助將不勝感激。
查看完整描述

2 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

這應該可以幫助您實現目標。它涉及正則表達式的一些高級用法。


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

        )


)



查看完整回答
反對 回復 2023-04-15
?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

這個解決方案可以解決嗎?


$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                                                                                                                                 

)               


查看完整回答
反對 回復 2023-04-15
  • 2 回答
  • 0 關注
  • 166 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號