T
T
Twist2014-05-25 13:32:28
PHP
Twist, 2014-05-25 13:32:28

How to glue an array?

How to glue an array so that it looks like this

Array
(
    [0] => Array
        (
            [column] => teplo
            [da] => 5
        )

    [1] => Array
        (
            [column] => teplo
            [net] => 2
        )

    [2] => Array
        (
            [column] => sherst
            [est] => 3
        )

    [3] => Array
        (
            [column] => sherst
            [net] => 4
        )

    [4] => Array
        (
            [column] => kril
            [net] => 5
        )

    [5] => Array
        (
            [column] => kril
            [est] => 2
        )

    [6] => Array
        (
            [column] => moloko
            [est] => 4
        )

    [7] => Array
        (
            [column] => moloko
            [net] => 3
        )

    [8] => Array
        (
            [column] => yaico
            [net] => 3
        )

    [9] => Array
        (
            [column] => yaico
            [est] => 4
        )

)

bought this
Array
(
    [0] => Array
        (
            [column] => teplo
            [da] => 5
            [net] => 2
        )
    [1] => Array
        (
            [column] => sherst
            [est] => 3
            [net] => 4
        )

    [2] => Array
        (
            [column] => kril
            [net] => 5
            [est] => 2
        )

    [3] => Array
        (
            [column] => moloko
            [est] => 4
            [net] => 3
        )

    [4] => Array
        (
            [column] => yaico
            [net] => 3
            [est] => 4
        )
)

I spawned a bunch of some kind of crutch implementation without finding the right one, but I feel that the solution is quite simple

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Solovyov, 2014-05-25
@pavel_salauyou

loop through the first array and add the desired value from another array by index

V
Vladlen Grachev, 2014-05-25
@gwer

Input array declaration
// Объявление входного массива
$t0 = Array
(
    '0' => Array
        (
            'column' => teplo,
            'da' => 5
        ),

    '1' => Array
        (
            'column' => teplo,
            'net' => 2
        ),

    '2' => Array
        (
            'column' => sherst,
            'est' => 3
        ),

    '3' => Array
        (
            'column' => sherst,
            'net' => 4
        ),

    '4' => Array
        (
            'column' => kril,
            'net' => 5
        ),

    '5' => Array
        (
            'column' => kril,
            'est' => 2
        ),

    '6' => Array
        (
            'column' => moloko,
            'est' => 4
        ),

    '7' => Array
        (
            'column' => moloko,
            'net' => 3
        ),

    '8' => Array
        (
            'column' => yaico,
            'net' => 3
        ),

    '9' => Array
        (
            'column' => yaico,
            'est' => 4
        )
);

// Немного магии
$t1 = array_values(array_map('unserialize', array_unique(array_map('serialize', array_map(function($a){return array_merge($a[0], $a[1]);}, array_map(function($a_m) use ($t0){return array_values(array_filter($t0, function($a_f) use ($a_m){return $a_f['column']==$a_m['column'];}));}, $t0))))));

// Вывод результата, полностью соответствующего требуемому
print_r($t1);

formatted version
$t1 = array_values(
  array_map(
    'unserialize', 
    array_unique(
      array_map(
        'serialize', 
        array_map(
          function($a){
            return array_merge($a[0], $a[1]);
          }, 
          array_map(
            function($a_m) use ($t0){
              return array_values(
                array_filter(
                  $t0, 
                  function($a_f) use ($a_m){
                    return $a_f['column']==$a_m['column'];
                  }
                )
              );
            }, 
            $t0
          )
        )
      )
    )
  )
);

N
Nazar Mokrinsky, 2014-05-25
@nazarpc

$result_array = [];
foreach ($input_array as $array) {
    $column = $array['column'];
    $result_array[$column] = array_merge(
        $array,
        isset($result_array[$column]) ? $result_array[$column] : []
    );
}
$result_array = array_values($result_array);

Somehow it should work. Didn't check.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question