我在处理 PHP 数组时遇到问题,我有一个数组:
[0] {
'email' => '[email protected]',
'meta' => {
'product' => {
'id' => '1',
'content' => 'This is content'
}
}
}
[1] {
'email' => '[email protected]',
'meta' => {
'product' => {
'id' => '2',
'content' => 'This is content'
}
}
}
[2] {
'email' => '[email protected]',
'meta' => {
'product' => {
'id' => '3',
'content' => 'This is content'
}
}
}
我需要按值合并这个数组
'email'
,如下所示:
[0] {
'email' => '[email protected]',
'meta' => {
'product' => {
'id' => '1',
'content' => 'This is content'
}
}
}
[1] {
'email' => '[email protected]',
'meta' => {
'product' => [0] {
'id' => '2',
'content' => 'This is content'
}
[1] {
'id' => '3',
'content' => 'This is content'
}
}
}
有人可以帮助我吗?
$sorted_array = [];
$emails = [];
$i = 0;
foreach ($arr as $array) {
if(!empty($array['email']) && !empty($array['meta']['product'])){
if( in_array($array['email'], $emails)){
$i--;
} else {
$emails[] = $array['email'];
}
$sorted_array[$i]['email'] = $array['email'];
$sorted_array[$i]['meta']['product'][] = $array['meta']['product'];
$i++;
}
}
echo "<pre>";
print_r($sorted_array);
希望这对你有帮助
Php 有很多对数组进行排序的函数。您可以查阅文档来选择适合您情况的更好算法http://php.net/manual/en/array.sorting.php。您可以使用 array_merge 函数合并结果数组,就像这样:
数组合并($a1,$a2)
我在这里为您的代码创建了一个示例:
您可以像数组的键一样使用电子邮件,最后,使用 array_combine 获得从 0 到 N 的下标...
<?php
$oldArray = array(
array(
'email' => '[email protected]',
'meta' => array(
'product' => array(
'id' => '1',
'content' => 'This is content'
)
)
), array(
'email' => '[email protected]',
'meta' => array(
'product' => array(
'id' => '2',
'content' => 'This is content'
)
)
), array(
'email' => '[email protected]',
'meta' => array(
'product' => array(
'id' => '3',
'content' => 'This is content'
)
)
)
);
$newArray = array();
foreach($oldArray as $element){
if(isset($newArray[$element['email']])) {
if(!isset($newArray[$element['email']]['meta']['product'][0]))
$newArray[$element['email']]['meta']['product'] = array($newArray[$element['email']]['meta']['product']);
$newArray[$element['email']]['meta']['product'][] = $element['meta']['product'];
} else {
$newArray[$element['email']] = $element;
}
}
//For index since 0 to n
print_r(array_combine(range(0,count($newArray)-1), $newArray));
为了获得最佳效率,请使用单个循环并对结果数组进行基于键的检查,以确定之前是否遇到过某个组。 当随后遇到电子邮件时,通过将初始元产品数据推送为新的子元素,确保将非列表(非索引)子数组转换为“列表数组”,然后将新数据推送为一个新孩子。
使用
array_values()
重新索引临时密钥 演示
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['email']])) {
$result[$row['email']] = $row;
continue;
}
if (!array_is_list($result[$row['email']]['meta']['product'])) {
$result[$row['email']]['meta']['product'] = [$result[$row['email']]['meta']['product']];
}
$result[$row['email']]['meta']['product'][] = $row['meta']['product'];
}
var_export(array_values($result));
或者推送引用以避免重新索引演示
$result = [];
foreach ($array as $row) {
if (!isset($ref[$row['email']])) {
$ref[$row['email']] = $row;
$result[] =& $ref[$row['email']];
continue;
}
if (!array_is_list($ref[$row['email']]['meta']['product'])) {
$ref[$row['email']]['meta']['product'] = [$ref[$row['email']]['meta']['product']];
}
$ref[$row['email']]['meta']['product'][] = $row['meta']['product'];
}
var_export($result);
您可以使用排序后的信息构建一个新数组。
它应该可以使用类似的东西,但未经测试:
$sorted_array = [];
$i = 0;
foreach ($unsorted_array as $array) {
if(!empty($array['email']) && !empty($array['meta']['product'])){
$sorted_array[$i]['email'] = $array['email'];
$sorted_array[$i]['meta']['product'][] = $array['meta']['product'];
$i++;
}
}
print_r($sorted_array);