将重复元素组合为多维数组中的数组

I was wondering when working with multimedional arrays, if a certain key is the same, is there a way to combine the contents of other keys into its own array if a certain key is the same?

Something like this:

// name is the same in both arrays
array(
    array(
        'name' => 'Pepsi',
        'store' => 'Over here',
        'number' => '1234567'
    ),
    array(
        'name' => 'Pepsi',
        'store' => 'Over here',
        'number' => '5556734'
    )
)

into something like this

array(
    array(
        'name' => 'Pepsi',
        'store' => array('Over here', 'Over here'),
        'number' => array('1234567', '5556734')
    )
)

The defining key is checking if the name element is the same for the other arrays.

Thanks to Gianni Lovece for her answer but I was able to develop a much simpler solution based on this problem. Just plug in the $result_arr to browse through and the $key you want to use as basis and it immediately outputs a multidimensional array with non-repeating values for repeating elements (see example below).

function multiarray_merge($result_arr, $key){

    foreach($result_arr as $val){
        $item = $val[$key];     
        foreach($val as $k=>$v){
            $arr[$item][$k][] = $v;
        }
    }

    // Combine unique entries into a single array
    // and non-unique entries into a single element
    foreach($arr as $key=>$val){
        foreach($val as $k=>$v){
            $field = array_unique($v);
            if(count($field) == 1){
                $field = array_values($field);
                $field = $field[0];
                $arr[$key][$k] = $field;
            } else {
                $arr[$key][$k] = $field;
            }
        }
    }
    return $arr;
}

For example, in the sample array for this question, running multiarray_merge($mysample, 'name') returns

array(
    'Pepsi' => array(
        'name' => 'Pepsi',
        'store' => 'Over here', // String: Not an array since values are not unique
        'number' => array('1234567', '5556734') // Array: Saved as array since values are unique
    )
);

You can try a function like this.

function mergeByKey($array,$key){
  $tmp_array = array();
  foreach ( $array as $k => $row ) {
    $merged = false;
    foreach ($tmp_array as $k2 => $tmp_row){
       if ($row[$key] == $tmp_row[$key]){
          foreach ( $row as $k3 => $value ) {
            if ($k3 == $key) continue;
            $tmp_array[$k2][$k3][] = $value;
            $merged = true;
          }
       }
       if ($merged) break;
    }
    if (!$merged) {
       $new_row = array();
       foreach ( $row as $k4 => $value ) {
         if ($k4 == $key) $new_row[$k4] = $value;
         else $new_row[$k4] = array($value);
       }
       $tmp_array[] = $new_row;
    }
  }
  foreach ( $tmp_array as $t => $row ) {
    foreach ( $row as $t2 => $value ) {
      if ( count($value) == 1 && $t2 != $key ) $tmp_array[$t][$t2] = $value[0];
    }
  }
  return $tmp_array;
}

passing the array as first parameter and the key as second one. I'm referencing to your array structure

edited: missed a piece

edited2: if resultin array contains elements with one string, it returns a string and not a array with one element

demo

This function uses a given field name as the grouping identifier and turns all other fields into arrays.

Note that single occurrences of your field name will yield arrays with a single element for the other fields. I wasn't sure whether that's a desirable trait, but just making sure you know ;-)

$arr = array(
    array(
        'name' => 'Pepsi',
        'store' => 'Over here',
        'number' => '1234567'
    ),
    array(
        'name' => 'Pepsi',
        'store' => 'Over here',
        'number' => '5556734'
    )
);

function mergeArray($array, $column)
{
    $res = array();

    foreach ($array as $item) {
        foreach ($item as $key => $value) {
            if ($key === $column) {
                $res[$column][$key] = $value;
            } else {
                $res[$column][$key][] = $value;
            }
        }
    }
    return array_values($res);
}

print_r(mergeArray($arr, 'name'));

Demo