将一个包含指定值的数组合并为一个键。

3

现在我有这样的数组集合:

[9] => Array
    (
        [sl] => 10
        [upload_dt] => 2015-04-15 14:39:58
        [total_files] => 3
        [file_name] => logo01.png
        [remarks] => qqq
        [status] => pending
        [download_file] => http://localhost/web/download_file21
    )

[10] => Array
    (
        [sl] => 10
        [upload_dt] => 2015-04-15 14:39:58
        [total_files] => 3
        [file_name] => face.jpg
        [remarks] => 5645645
        [status] => pending
        [download_file] => http://localhost/web/download_file22
    )

[11] => Array
    (
        [sl] => 10
        [upload_dt] => 2015-04-15 14:39:58
        [total_files] => 3
        [file_name] => ID_11401871809904(15).pdf
        [remarks] => 567567
        [status] => pending
        [download_file] => http://localhost/web/download_file23
    )

现在,我需要将一些索引中相同的值合并为一个数组。

最终合并后的数组应该如下所示。

[9] => Array
    (
        [sl] => 10
        [upload_dt] => 2015-04-15 14:39:58
        [total_files] => 3
        [file_name] => logo01.png , face.jpg, ID_11401871809904(15).pdf
        [remarks] => qqq, 5645645 , 567567
        [status] => pending, pending ,pending
        [download_file] => http://localhost/web/download_file21,
                           http://localhost/web/download_file22,
                       http://localhost/web/download_file23
    )

我尝试使用array_merge,但在这种情况下它实际上并没有起作用。


只需使用array_merge。 - Sunil Pachlangia
@SunilPachlangia 我已经尝试过了,但它没有起作用。 - dude
你需要将具有相同 s1 值的元组数组元素进行分组吗?你必须明确说明这一点。 - animaacija
3个回答

2
解决方案将如下所示:
<?php
    $result = array();
    $ar1[9] = Array
                (
                    'sl' => 10,
                    'upload_dt' => '2015-04-15 14:39:58',
                    'total_files' => 3,
                    'file_name' => 'logo01.png',
                    'remarks' => 'qqq',
                    'status' => 'pending',
                    'download_file' => 'http://localhost/web/download_file21'
                );
    $ar1[10] = Array
                (
                    'sl' => 10,
                    'upload_dt' => '2015-04-15 14:39:58',
                    'total_files' => 3,
                    'file_name' => 'face.jpg',
                    'remarks' => '5645645',
                    'status' => 'pending',
                    'download_file' => 'http://localhost/web/download_file22'
                );

    $ar1[11] =  Array
                (
                    'sl' => 10,
                    'upload_dt' => '2015-04-15 14:39:58',
                    'total_files' => 3,
                    'file_name' => 'ID_11401871809904(15).pdf',
                    'remarks' => 567567,
                    'status' => 'pending',
                    'download_file' => 'http://localhost/web/download_file23'
                );

    foreach($ar1 as $record){
        $keys = array_keys($record); 
        foreach($keys as $key) { 
            if(array_key_exists($key,$result)){
                $valeInKey = explode(',', $result[$key]);
                if (!in_array($record[$key], $valeInKey)){
                    $result[$key]= $result[$key] .",".$record[$key];
                }
            } else{
                $result[$key]= $record[$key];
            } 
        }

    }
    echo"<pre>";print_r($result);exit;

    ?>

这是一个单一的数组,而不是多个数组。 - dude
你可以根据需要更改格式,例如: $ar1 = array( 9 => Array ( -------------------- ), 10 => Array ( -------------------- ), 11 => Array ( -------------------- ) ); - user2590920

1
你实际上是在这些数组中合并字符串,因此放弃array_merge的想法,使用简单循环然后使用键作为基础来连接字符串。粗略示例:
$result = array();
foreach($array as $values) {
    if(!isset($result[$values['sl']])) {
        $result[$values['sl']] = $values; // initial
    } else {
        foreach(array('file_name', 'remarks', 'status', 'download_file') as $field) {
            $result[$values['sl']][$field] .= ", {$values[$field]}";
        }

    }
}

样例输出


1
为什么你刚才只是复制了我的答案。 - user2590920
@ShikhaSinghal 请问?抄袭了你的答案?我干嘛要这么做呢? - Kevin
@Ghost 但是在输出之前我得到了空数组。 - dude
@哥们儿,你说的空数组是什么意思?我只是使用了你提供的数据,检查一下样本输出就行了。它与上面的数据相同,除非你使用的数据确实与发布的不同。 - Kevin

0

array_merge 不会起作用,因为键会互相覆盖。
解决方案是使用 foreach 来合并它们。就像这样:

$new = array();
foreach($arr as $key => $value) {
    $new[$key] .= ", ".$value;
}

但是如果您想要合并多个数组,那么每个数组都需要有多个条目。如果您只想对其中几个进行操作,则需要检查键并相应地执行某些操作。


甚至可以使用foreach($arr as $key => $value) { $new[$key][] = $value; },然后使用foreach $new -> arrayUNIQUE() -> implode.(', ')。你明白我的意思! - animaacija

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接