Answer the question
In order to leave comments, you need to log in
How to group an array by name?
Array
(
[0] => Array
(
[name] => Холопова Анастасия
[num_meet1] => 4
)
[1] => Array
(
[name] => Погарченкова Мария
[num_meet1] => 2
)
[2] => Array
(
[name] => Индутова Валерия
[num_meet1] => 1
)
[3] => Array
(
[name] => Валерьянова Саша
[num_meet2] => 1
)
[4] => Array
(
[name] => Валерьянова Саша
[num_meet3] => 1
)
[5] => Array
(
[name] => Холопова Анастасия
[num_meet4] => 1
)
)
[0] => Array (
[name] => Холопова Анастасия
[num_meet1] => 4
[num_meet2] =>
[num_meet3] =>
[num_meet4] => 1
[num_meet5] =>
[num_meet6] =>
)
[1] => Array
(
[name] => Валерьянова Саша
[num_meet1] =>
[num_meet2] => 1
[num_meet3] => 1
[num_meet4] =>
[num_meet5] =>
[num_meet6] =>
)
)
Answer the question
In order to leave comments, you need to log in
Sorry, no PHP handy. Posted from a tablet.
/**
*
* Input array consists of elements as in example
* <code>
* Array
* (
* [0] => Array
* (
* [name] => Холопова Анастасия
* [num_meet1] => 4
* )
*
* [1] => Array
* (
* [name] => Погарченкова Мария
* [num_meet1] => 2
* )
*
* [2] => Array
* (
* [name] => Индутова Валерия
* [num_meet1] => 1
* )
*
* [3] => Array
* (
* [name] => Валерьянова Саша
* [num_meet2] => 1
* )
*
* [4] => Array
* (
* [name] => Валерьянова Саша
* [num_meet3] => 1
* )
*
* [5] => Array
* (
* [name] => Холопова Анастасия
* [num_meet4] => 1
* )
*
* )
* </code>
*
* @param array $array
*
* @return array
*/
function alterArray(array $array){
$newArray = [];
foreach($array as $subarray)
{
if(isset($subarray['name']))
{
$lastElementOfArray = array_slice($subarray, 1, 1);
reset($lastElementOfArray);
$newArray[ $subarray['name'] ][ key($lastElementOfArray) ] = current($lastElementOfArray);
}
}
return $newArray;
}
array_combine(
array_map($array, function($a) {return $a['name'];}),
$array
);
just go through foreach it, if there is a repetition, then add it to the existing one, and delete this one
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question