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
)
)
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
)
- 2 回答
- 0 关注
- 130 浏览
添加回答
举报