Answer the question
In order to leave comments, you need to log in
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
)
)
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
)
)
Answer the question
In order to leave comments, you need to log in
loop through the first array and add the desired value from another array by index
// Объявление входного массива
$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);
$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
)
)
)
)
)
);
$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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question