我有这种情况:
$array = [
"name" => [
"ro" => [
0 => "Nume Romana",
],
"en" => [
0 => "Nume Engleza",
],
"fr" => [
0 => "Nume Franceza",
],
],
"meta_keywords" => [
"ro" => [
0 => "keywords romana",
],
"en" => [
0 => "keywords Engleza",
],
"fr" => [
0 => "keywords Franceza",
],
],
"meta_description" => [
"ro" => [
0 => "desciption romana",
],
"en" => [
0 => "desciption Engleza",
],
"fr" => [
0 => "desc Franceza",
],
],
];
我想这样分组:
$result = [
"ro" => [
"name" => "Nume Romana",
"meta_keywords" => "keywords romana",
"meta_description" => "desciption romana",
],
"en" => [
"name" => "Nume Romana",
"meta_keywords" => "keywords romana",
"meta_description" => "desciption romana",
],
];
我尝试了 array_merge_recursive()、foreach、array_combine(),但没有结果。
您可以使用嵌套的 foreach 循环遍历数组并重新格式化数据:
$result = [];
foreach ($array as $key => $value) {
foreach ($value as $language => $translation) {
$result[$language][$key] = $translation[0];
}
}
这将产生所需的输出:
$result = [
"ro" => [
"name" => "Nume Romana",
"meta_keywords" => "keywords romana",
"meta_description" => "desciption romana",
],
"en" => [
"name" => "Nume Engleza",
"meta_keywords" => "keywords Engleza",
"meta_description" => "desciption Engleza",
],
"fr" => [
"name" => "Nume Franceza",
"meta_keywords" => "keywords Franceza",
"meta_description" => "desc Franceza",
],
];