我正在使用 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
];
}
}
任何帮助将不胜感激。
蝴蝶不菲
狐的传说