3 回答
TA贡献1802条经验 获得超4个赞
你可以用array_filter与ARRAY_FILTER_USE_KEY标志根据是否与“translation_”与否的关键开始对不同的元素分开:
$translationArray = array_filter($myArray,
function ($k) {
return substr($k, 0, 12) == 'translation_';
}, ARRAY_FILTER_USE_KEY);
$baseData = array_filter($myArray,
function ($k) {
return substr($k, 0, 12) != 'translation_';
}, ARRAY_FILTER_USE_KEY);
var_export($translationArray);
var_export($baseData);
输出:
array (
'translation_lang_id' => 3,
'translation_name' => 'Lorem Ipsum',
'translation_description' => 'My content',
)
array (
'id' => 1,
'code' => '1234',
)
TA贡献1810条经验 获得超5个赞
一种蛮力方法是迭代数组,并将元素分配给适当的目标数组:
$translationArray = array();
$baseData = array();
foreach ($myArray as $key => $value) {
if (preg_match("/^translation_/", $key)) {
$translationArray[$key] = $value;
}
else {
$baseData[$key] = $value;
}
}
- 3 回答
- 0 关注
- 128 浏览
添加回答
举报