2

so I have an array like this :-

$headers = array();
$headers[] = array("country" => "Netherlands", "city" => "Amsterdam");
$headers[] = array("country" => "Netherlands", "city" => "Tillsburg");
$headers[] = array("country" => "Sweden", "city" => "Stockholm");

I need it like :-

array("country" => "Netherlands", "city" => "Amsterdam,Tillsburg");
array("country" => "Sweden", "city" => "Stockholm");

How do I merge the values?

Here's what I have tried till now, but it doesn't seems to be working.

function merge($headers) {
    $final = [];
    foreach($headers as $current) {
        if(!in_array($current['country'], $final)) {
            $final[] = $current['country'];
        }
  }

  foreach($headers as $current) {
      $final[$current['country']]['city'] = $current['city'];
  }
  return $final;
}

Any help would be appreciated.

1

4 Answers 4

0

Use country values as temporary associative keys to determine if concatenation is necessary.

Code: (Demo)

$headers[] = array("country" => "Netherlands", "city" => "Amsterdam");
$headers[] = array("country" => "Netherlands", "city" => "Tillsburg");
$headers[] = array("country" => "Sweden", "city" => "Stockholm");

foreach ($headers as $row) {
    if (!isset($result[$row['country']])) {
        $result[$row['country']] = $row; // store the whole row
    } else {
        $result[$row['country']]['city'] .= ",{$row['city']}";  // concatenate
    }
}

var_export(array_values($result)); // reindex the output array

Output:

array (
  0 => 
  array (
    'country' => 'Netherlands',
    'city' => 'Amsterdam,Tillsburg',
  ),
  1 => 
  array (
    'country' => 'Sweden',
    'city' => 'Stockholm',
  ),
)
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe this is not the best solution, but I test it, it works.

function merge($headers) {
    $final = array();
    foreach ($headers as $current) {
        if( !isset( $final[ $current['country'] ] ) ) {
            $final[$current['country']] = [
                'country' => $current['country'],
                'city'    => $current['city']
            ];
            continue;
      }
      $final[ $current['country'] ]['city'] = $final[ $current['country'] ]['city'] . ', ' . $current['city'];
    }

    return $final;
}

Output

array(2) { ["Netherlands"]=> array(2) { ["country"]=> string(11) "Netherlands" ["city"]=> string(20) "Amsterdam, Tillsburg" } ["Sweden"]=> array(2) { ["country"]=> string(6) "Sweden" ["city"]=> string(9) "Stockholm" } }

You can clear keys of result array if you want adding

$final = array_values($final);

before return statement

Demo

1 Comment

0
$headers = array();
$headers[] = array("country" => "Netherlands", "city" => "Amsterdam");
$headers[] = array("country" => "Netherlands", "city" => "Tillsburg");
$headers[] = array("country" => "Sweden", "city" => "Stockholm");
$result = array();

foreach($headers as $key => $value) {
    $result[$value['country']][] = $value['city'];
}
$finalArray = array();
foreach($result as $k => $v) {
    $finalArray[] = array('country' => $k, 'city' => join(',', $v));
}

1 Comment

Two loops is not optimal design.
-1

Create another array with country name as key

<?php
$headers = array();
$headers[] = array("country" => "Netherlands", "city" => "Amsterdam");
$headers[] = array("country" => "Netherlands", "city" => "Tillsburg");
$headers[] = array("country" => "Sweden", "city" => "Stockholm");
$countries = array_unique(array_column($headers,"country"));

$newHeaders = array();
foreach($headers as $header){
    $newHeaders[$header["country"]]["city"][] = $header["city"];
}
print_r($newHeaders);

foreach($newHeaders as $key=>$headers){
    echo $key." City : ".implode(",",$headers['city'])."\n";
}
?>

Live Demo Link

Output would be like

Netherlands City : Amsterdam,Tillsburg
Sweden City : Stockholm 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.