将多个值转换为单个键

  • 本文关键字:单个键 转换 php arrays
  • 更新时间 :
  • 英文 :


我使用php 7.1,我有以下数组:

<?php
$arr = array (
0 =>
array (
'Product' => 'Product1',
'Exact_Match_Keyword' => 'apples freshness',
),
1 =>
array (
'Product' => 'Product1',
'Exact_Match_Keyword' => 'apples',
),
2 =>
array (
'Product' => 'Product1',
'Exact_Match_Keyword' => 'oranges',
),
3 =>
array (
'Product' => 'Product2',
'Exact_Match_Keyword' => 'apples freshness',
),
4 =>
array (
'Product' => 'Product2',
'Exact_Match_Keyword' => 'apples',
),
);
$arr = array_merge_recursive($arr);
var_dump($arr);
/* WANTED DATA STRUCTURE
$arr = array (
0 =>
array (
'Product' => 'Product1',
'Exact_Match_Keyword' => array('apples freshness', 'apples', 'oranges'),
),
1 =>
array (
'Product' => 'Product2',
'Exact_Match_Keyword' => array('apples freshness', 'apples'),
),
);
*/

我尝试使用array_merge_recursive,但是这并没有给我想要的数据结构。

有什么建议我做错了吗?

感谢您的回复!

您可以使用array_reduce来为您处理这样的简单逻辑。下面的示例将提供您所发布的预期格式。

$example = array_values(
array_reduce(
$arr,
static function ($acc, $row) {
if (!isset($acc[$row['Product']])) {
$acc[$row['Product']] = [
'Product' => $row['Product'],
'Exact_Match_Keyword' => [$row['Exact_Match_Keyword']],
];
} else {
$acc[$row['Product']]['Exact_Match_Keyword'] = array_merge(
$acc[$row['Product']]['Exact_Match_Keyword'],
[$row['Exact_Match_Keyword']]
);
}
return $acc;
}
));
print_r($example);

如果您期望的格式是灵活的,可以进一步简化:

$example = array_reduce(
$arr,
static function ($acc, $row) {
if (!isset($acc[$row['Product']])) {
$acc[$row['Product']] = [$row['Exact_Match_Keyword']];
} else {
$acc[$row['Product']] = array_merge(
$acc[$row['Product']],
[$row['Exact_Match_Keyword']]
);
}
return $acc;
}
);
print_r($example);
//Output: 
//[
//    'Product1'=> ['apples freshness', 'apples', 'oranges'],
//    'Product2'=> ['apples freshness', 'apples']
//]

我认为这得到了你想要的。这可能不是世界上最优雅的事情,但这大致是你想要遵循的逻辑。

$temp = [];
foreach ($arr as $v)
{
$pname = $v['Product'];
if (!isset($temp[$pname]))
{
$temp[$pname] = []; 
} 
$temp[$pname][] = $v['Exact_Match_Keyword'];
}
$out = [];
foreach ($temp as $k => $v)
{
$out[] = [
'Product' => $k,
'Exact_Match_Name' => $v
];
}

php > var_dump($out);
php shell code:1:
array(2) {
[0] =>
array(2) {
'Product' =>
string(8) "Product1"
'Exact_Match_Name' =>
array(3) {
[0] =>
string(16) "apples freshness"
[1] =>
string(6) "apples"
[2] =>
string(7) "oranges"
}
}
[1] =>
array(2) {
'Product' =>
string(8) "Product2"
'Exact_Match_Name' =>
array(2) {
[0] =>
string(16) "apples freshness"
[1] =>
string(6) "apples"
}
}
}

为了将来的参考,如果您想单独使用array_merge_recursive()方法,则输入数据可能必须与我所知道的略有不同。

在查看了array_merge_recursive()的文档页面中的示例之后,我想知道原始输入数据是否需要稍微不同的结构。我认为需要有匹配的内部数组键以便最终的数组有东西可以组合各种内部键。在下面的示例数据中,我使用Products键来组合第一个内部键。

我不知道你的起始数据是否有可能在这个过程中被结构化,但如果数据看起来像下面(基于array_merge_recursive()文档页面上的例子):

<?php
$ar1 = array(
"Products" => array(
"Product1" => array(
"Exact_Match_Keyword" => array('apples freshness')
)
)
);
$ar2 = array(
"Products" => array(
"Product1" => array(
"Exact_Match_Keyword" => array('apples')
)
)
);
$ar3 = array(
"Products" => array(
"Product1" => array(
"Exact_Match_Keyword" => array('oranges')
)
)
);
$ar4 = array(
"Products" => array(
"Product2" => array(
"Exact_Match_Keyword" => array('apples freshness')
)
)
);
$ar5 = array(
"Products" => array(
"Product2" => array(
"Exact_Match_Keyword" => array('apples')
)
)
);
$result = array_merge_recursive($ar1, $ar2, $ar3, $ar4, $ar5);
print_r($result);

输出应该像这样:

Array
(
[Products] => Array
(
[Product1] => Array
(
[Exact_Match_Keyword] => Array
(
[0] => apples freshness
[1] => apples
[2] => oranges
)
)
[Product2] => Array
(
[Exact_Match_Keyword] => Array
(
[0] => apples freshness
[1] => apples
)
)
)
)

最新更新