S
S
Sama Samsonov2018-04-12 07:42:38
PHP
Sama Samsonov, 2018-04-12 07:42:38

How to make one two-dimensional array from two arrays?

there are two arrays

$users = array(array('id' => 1, 'name' =>  "Александр"),
                 array('id' =>2, 'name' =>  "Сергей"),
                 array('id' => 3, 'name' =>  "Никита"),
                 array('id' => 4, 'name' =>  "Володя"));
 
  $punch = array(array('id' => 1,  'user_id' => 1, 'start' =>  "08:48:00", 'stop' =>  "12:00:00", 'date' =>  "2018-02-01"),
                 array('id' =>2, 'user_id' => 2, 'start' =>  "08:05:00", 'stop' =>  "18:00:00", 'date' =>  "2018-02-01"),
                 array('id' => 3, 'user_id' => 3, 'start' =>  "08:18:00", 'stop' =>  "13:20:00", 'date' =>  "2018-02-01"),
                 array('id' => 1, 'user_id' => 1, 'start' =>  "13:18:00", 'stop' =>  "18:48:00", 'date' =>  "2018-02-01"),
                 array('id' => 3, 'user_id' => 3, 'start' =>  "13:58:00", 'stop' =>  "18:48:00", 'date' =>  "2018-02-01"),
                 array('id' => 4, 'user_id' => 4, 'start' =>  "08:20:00", 'stop' =>  "18:15:00", 'date' =>  "2018-02-01"));
 
 Как мне из этих двух массива сделать один массив виде ?
 
 $multiarray = array('Александр'=>
                            array(
                                array( 'start' =>  "08:48:00", 'stop' =>  "12:00:00", 'date' =>  "2018-02-01"),
                                array( 'start' =>  "13:18:00", 'stop' =>  "18:48:00", 'date' =>  "2018-02-01")
                                ),
                    
                    'Сергей'=>
                            array(
                                array( 'start' =>  "08:05:00", 'stop' =>  "18:00:00", 'date' =>  "2018-02-01")
                                ),
                                
                    'Никита'=>
                            array(
                                array( 'start' =>  "08:18:00", 'stop' =>  "13:20:00", 'date' =>  "2018-02-01"),
                                array( 'start' =>  "13:58:00", 'stop' =>  "18:48:00", 'date' =>  "2018-02-01")
                                ),
                                
                    'Володя'=>
                            array(
                                array( 'start' =>  "08:20:00", 'stop' =>  "18:15:00", 'date' =>  "2018-02-01"),
                                array( 'start' =>  "13:58:00", 'stop' =>  "18:48:00", 'date' =>  "2018-02-01")
                                )
                    );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2018-04-12
@gromdron

In your case, there are 2 restrictions:
1) If you have 2 users with the same name, then their data will be mixed into one heap
2) If you change the $users format a little, then it will be much easier to get the resulting array (the first cycle for associativity will not be needed)

/**
 * Шаг 1 - разворачиваем $users в плоский массив
 * Если есть возможность получить его сразу ассоциативным,
 * то это было бы лучше
 */
$usersAssoc = [];

foreach ($users as $user)
{
  $usersAssoc[ $user['id'] ] = $user['name'];
}

/**
 * Шаг 2 - выполяем группирующее преобразование наших $punch
 */
$result = [];

foreach ($punch as $time)
{
  if ( !array_key_exists($time['user_id'], $usersAssoc) )
  {
    // В punch есть массив, который относится к пользователю
    // о окотором мы не знаем
    continue;
  }

  $result[ $usersAssoc[ $time['user_id'] ] ][ $time['id'] ] = $time;
}


echo "<pre>";
var_dump($result);
echo "</pre>";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question