如何以编程方式将数组数据添加到另一个数组

时间:2017-07-17 16:11:14

标签: php arrays

我有两个数组,第一个是保存用户的基本详细信息,例如用户类型,用户加入日期&点

// User data array
$data = array(array('0' => 'New User', '1' => '2017-07-10', '2' => '12'), array('0' => 'New User', '1' => '2017-07-11', '2' => '6'), array('0' => 'New User', '1' => '2017-07-13', '2' => '19'));

// Output:
Array
(
    [0] => Array
        (
            [0] => New User
            [1] => 2017-07-10
            [2] => 12
        )

    [1] => Array
        (
            [0] => New User
            [1] => 2017-07-11
            [2] => 6
        )

    [2] => Array
        (
            [0] => New User
            [1] => 2017-07-13
            [2] => 19
        )

)

,第二个是周日期数组:

// Dates array
$datesArr = array('0' => '2017-07-10', '1' => '2017-07-11', '2' => '2017-07-12', '3' => '2017-07-13', '4' => '2017-07-14', '5' => '2017-07-15', '6' => '2017-07-16', '7' => '2017-07-17');

// Output:
Array
(
    [0] => 2017-07-10
    [1] => 2017-07-11
    [2] => 2017-07-12
    [3] => 2017-07-13
    [4] => 2017-07-14
    [5] => 2017-07-15
    [6] => 2017-07-16
    [7] => 2017-07-17
)

现在我想添加用户$datesArr数组中缺少的$data数组的日期。

例如,以下日期缺失:

$data = array(array('0' => 'New User', '1' => '2017-07-12', '2' => '0'), array('0' => 'New User', '1' => '2017-07-14', '2' => '0'), array('0' => 'New User', '1' => '2017-07-15', '2' => '0'), array('0' => 'New User', '1' => '2017-07-16', '2' => '0'), array('0' => 'New User', '1' => '2017-07-17', '2' => '0'));

Array
(
    [0] => Array
        (
            [0] => New User
            [1] => 2017-07-12
            [2] => 0
        )

    [1] => Array
        (
            [0] => New User
            [1] => 2017-07-14
            [2] => 0
        )

    [2] => Array
        (
            [0] => New User
            [1] => 2017-07-15
            [2] => 0
        )

    [3] => Array
        (
            [0] => New User
            [1] => 2017-07-16
            [2] => 0
        )

    [4] => Array
        (
            [0] => New User
            [1] => 2017-07-17
            [2] => 0
        )

)

我正在看这个输出:

Array
(
    [0] => Array
        (
            [0] => New User
            [1] => 2017-07-10
            [2] => 12
        )

    [1] => Array
        (
            [0] => New User
            [1] => 2017-07-11
            [2] => 6
        )

    [2] => Array
        (
            [0] => New User
            [1] => 2017-07-12
            [2] => 0
        )

    [3] => Array
        (
            [0] => New User
            [1] => 2017-07-13
            [2] => 12
        )

    [4] => Array
        (
            [0] => New User
            [1] => 2017-07-14
            [2] => 0
        )

    [5] => Array
        (
            [0] => New User
            [1] => 2017-07-15
            [2] => 0
        )

    [6] => Array
        (
            [0] => New User
            [1] => 2017-07-16
            [2] => 0
        )

    [7] => Array
        (
            [0] => New User
            [1] => 2017-07-17
            [2] => 0
        )

)

知道如何将缺少日期从一个数组添加到另一个数组吗?

感谢。

5 个答案:

答案 0 :(得分:0)

诀窍是使用日期字符串作为数组键:

<?php
$users = [
    ['New User', '2017-07-10', 12],
    ['New User', '2017-07-11', 6],
    ['New User', '2017-07-13', 19]
];
$dates = [
    '2017-07-10',
    '2017-07-11',
    '2017-07-12',
    '2017-07-13',
    '2017-07-14',
    '2017-07-15',
    '2017-07-16',
    '2017-07-17'
];
$output = [];
// create new users array with dates as keys
array_walk($users, function($user) use (&$output) {
    $output[$user[1]] = $user;
});
// iterate over dates array to fill in as requred
array_walk($dates, function($date) use (&$output) {
    if (!array_key_exists($date, $output)) {
        $output[$date] = ['New User', $date, 0];
    }
});
print_r(array_values($output));

输出显然是:

Array
(
    [0] => Array
        (
            [0] => New User
            [1] => 2017-07-10
            [2] => 12
        )

    [1] => Array
        (
            [0] => New User
            [1] => 2017-07-11
            [2] => 6
        )

    [2] => Array
        (
            [0] => New User
            [1] => 2017-07-13
            [2] => 19
        )

    [3] => Array
        (
            [0] => New User
            [1] => 2017-07-10
            [2] => 0
        )

    [4] => Array
        (
            [0] => New User
            [1] => 2017-07-11
            [2] => 0
        )

    [5] => Array
        (
            [0] => New User
            [1] => 2017-07-12
            [2] => 0
        )

    [6] => Array
        (
            [0] => New User
            [1] => 2017-07-13
            [2] => 0
        )

    [7] => Array
        (
            [0] => New User
            [1] => 2017-07-14
            [2] => 0
        )

    [8] => Array
        (
            [0] => New User
            [1] => 2017-07-15
            [2] => 0
        )

    [9] => Array
        (
            [0] => New User
            [1] => 2017-07-16
            [2] => 0
        )

    [10] => Array
        (
            [0] => New User
            [1] => 2017-07-17
            [2] => 0
        )

)

答案 1 :(得分:0)

希望这会有所帮助,我们正在使用array_columnforeach来获得所需的输出。

Try this code snippet here

<?php
ini_set('display_errors', 1);
$data = array(
    array('0' => 'New User', '1' => '2017-07-10', '2' => '12'), 
    array('0' => 'New User', '1' => '2017-07-11', '2' => '6'), 
    array('0' => 'New User', '1' => '2017-07-13', '2' => '19'));
$datesArr = array(
    '0' => '2017-07-10', 
    '1' => '2017-07-11', 
    '2' => '2017-07-12', 
    '3' => '2017-07-13', 
    '4' => '2017-07-14', 
    '5' => '2017-07-15', 
    '6' => '2017-07-16', 
    '7' => '2017-07-17');

$datesPresent=array_column($data, 1);
$result=array();
foreach($datesArr as $date)
{
    if(!in_array($date, $datesPresent))
    {
        $result[]=array('New User',$date,0);
    }
    else
    {
        $result[]=current($data);
        next($data);
    }
}
print_r($result);

答案 2 :(得分:0)

你需要这样的东西吗?

<?php
$data = array(array('0' => 'New User', '1' => '2017-07-10', '2' => '12'), array('0' => 'New User', '1' => '2017-07-11', '2' => '6'), array('0' => 'New User', '1' => '2017-07-13', '2' => '19'));

$datesArr = array('0' => '2017-07-30', '1' => '2017-07-11', '2' => '2017-07-12', '3' => '2017-07-13', '4' => '2017-07-14', '5' => '2017-07-15', '6' => '2017-07-16', '7' => '2017-07-17');

$new_array = array();
foreach($datesArr as $date)
{
   foreach($data as $d)
   {
        if($date == $d[1])
        {
            $temp = $d;
            break;
        }
   }

   $temp = !isset($temp) ? array('0' => 'New User','1' => $date,'2' => 0) : $temp;
   $new_array[] = $temp;
}

var_dump($new_array);

答案 3 :(得分:0)

我将尝试在这里使用array_search:

$data = array(array('0' => 'New User', '1' => '2017-07-10', '2' => '12'), array('0' => 'New User', '1' => '2017-07-11', '2' => '6'), array('0' => 'New User', '1' => '2017-07-13', '2' => '19'));
$datesArr = array('0' => '2017-07-10', '1' => '2017-07-11', '2' => '2017-07-12', '3' => '2017-07-13', '4' => '2017-07-14', '5' => '2017-07-15', '6' => '2017-07-16', '7' => '2017-07-17');

$newData = array();
foreach ($datesArr as $value) {
  $key = array_search($value, array_column($data, 1));
  if ($key===false) {
    $newData[]=array('0' => 'New User', '1' => $value, '2' => 0);
  }else{
    $newData[]=$data[$key];
  }
}
print_r($newData);

答案 4 :(得分:0)

这是一个基于函数,无条件的方法处理,只需两行。

$defaults使用$datesArr中的日期生成一个数组,其中日期为关键字,子数组为New User,各自的日期和每个子数组中的0。< / p>

$merged使用array_merge()根据键(日期)覆盖默认子数组,然后array_values()重新索引子数组(删除临时日期键)。

代码:(Demo

$data=[
    ['0'=>'New User','1'=>'2017-07-10','2'=>'12'],
    ['0'=>'New User','1'=>'2017-07-11','2'=>'6'], 
    ['0'=>'New User','1'=>'2017-07-13','2'=>'19']
];
$datesArr=[
    '0'=>'2017-07-10', 
    '1'=>'2017-07-11', 
    '2'=>'2017-07-12', 
    '3'=>'2017-07-13', 
    '4'=>'2017-07-14', 
    '5'=>'2017-07-15', 
    '6'=>'2017-07-16', 
    '7'=>'2017-07-17'
];

$defaults=array_combine($datesArr,array_map(function($v){return ['New User',$v,'0'];},$datesArr)); // set default values
$merged=array_values(array_merge($defaults,array_column($data,null,1)));  // array_column re-assigns keys using dates
var_export($merged);

输出:

array (
  0 => 
  array (
    0 => 'New User',
    1 => '2017-07-10',
    2 => '12',
  ),
  1 => 
  array (
    0 => 'New User',
    1 => '2017-07-11',
    2 => '6',
  ),
  2 => 
  array (
    0 => 'New User',
    1 => '2017-07-12',
    2 => '0',
  ),
  3 => 
  array (
    0 => 'New User',
    1 => '2017-07-13',
    2 => '19',
  ),
  4 => 
  array (
    0 => 'New User',
    1 => '2017-07-14',
    2 => '0',
  ),
  5 => 
  array (
    0 => 'New User',
    1 => '2017-07-15',
    2 => '0',
  ),
  6 => 
  array (
    0 => 'New User',
    1 => '2017-07-16',
    2 => '0',
  ),
  7 => 
  array (
    0 => 'New User',
    1 => '2017-07-17',
    2 => '0',
  ),
)