T
T
tigra2015-09-21 10:50:10
PHP
tigra, 2015-09-21 10:50:10

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
        )

)

there is such an array, it is necessary to group in such a way that if a name is encountered, then it becomes one and the remaining values ​​​​are transferred to this element of the array. Those.
[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] => 
        )
)

num_meet1 is the number of meetings for the current month month, and so for half a year num_meet1-num_meet6
I found several options here, but there is a certain number in the array element, but here there may be different

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alex Safonov, 2015-09-21
@tigroid3

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;
    }

I
Ivan, 2015-09-21
@dohlik

array_combine(
    array_map($array, function($a) {return $a['name'];}), 
    $array
);

array_combine() creates an array from the passed arguments (the first is the array of keys, the second is the values). to extract keys we use array_map.
Naturally, this is a very simplified version, based on the assumption that there is always a name key, and the values ​​\u200b\u200bin it are unique.

L
LittleFatNinja, 2015-09-21
@LittleFatNinja

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 question

Ask a Question

731 491 924 answers to any question