2 回答

TA貢獻(xiàn)1810條經(jīng)驗 獲得超4個贊
這應(yīng)該可以幫助您實現(xiàn)目標(biāo)。它涉及正則表達(dá)式的一些高級用法。
//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);
應(yīng)該輸出:
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貢獻(xiàn)1804條經(jīng)驗 獲得超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 關(guān)注
- 162 瀏覽
添加回答
舉報