dude
dude

Reputation: 4982

Merge an array with specified values into one key

Right now, I have array sets like these:

[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
    )

Now, I need to merge same values in some of the indices into one arrays.

The merged array values should look like this in the end.

[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
    )

Now, I tried using array_merge but it didn't actually work in this case.

Upvotes: 3

Views: 61

Answers (3)

Kevin
Kevin

Reputation: 41893

You're actually merging strings inside those arrays so just ditch the array_merge idea, and just use a simple loop then use the key as your basis to concatenate strings. Rough example:

$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]}";
        }

    }
}

Sample Output

Upvotes: 1

user2590920
user2590920

Reputation:

Solution will be like this:

<?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;

    ?>

Upvotes: 2

Shimu
Shimu

Reputation: 1147

array_merge won't work, because the keys would overwrite each other.
A solution would be to merge them with a the help of a foreach. Something like this:

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

But you would have multiple entries for every single array you want to merge. If you just want to have them for a few, you have to check the key and do something accordingly.

Upvotes: 0

Related Questions