Home >Backend Development >PHP Tutorial >How to add data to interval array in PHP

How to add data to interval array in PHP

WBOY
WBOYOriginal
2016-07-06 13:53:331019browse

The existing array is as follows:
[site003] => Array

<code>    (
        [0] => Array
            (
                [key] => site003
                [riqi] => 2016-06-14
                [shijian] => 00
                [num] => 1
            )

        [1] => Array
            (
                [key] => site003
                [riqi] => 2016-06-14
                [shijian] => 04
                [num] => 2
            )

        [2] => Array
            (
                [key] => site003
                [riqi] => 2016-06-14
                [shijian] => 07
                [num] => 6
            )

    )</code>

Since there are no 01, 02, and 03 time points in the time shijian field from 00 to -04, I want to supplement the num field of the assembled data at 01, 02, and 03 time points with 0,
The latter is between 04-07 At time points 05 and 06, the time point of num is also added to the array as 0
Please give some guidance from experts! Thank you, brother!

Reply content:

The existing array is as follows:
[site003] => Array

<code>    (
        [0] => Array
            (
                [key] => site003
                [riqi] => 2016-06-14
                [shijian] => 00
                [num] => 1
            )

        [1] => Array
            (
                [key] => site003
                [riqi] => 2016-06-14
                [shijian] => 04
                [num] => 2
            )

        [2] => Array
            (
                [key] => site003
                [riqi] => 2016-06-14
                [shijian] => 07
                [num] => 6
            )

    )</code>

Since there are no 01, 02, and 03 time points in the time shijian field from 00 to -04, I want to supplement the num field of the assembled data at 01, 02, and 03 time points with 0,
The latter is between 04-07 At time points 05 and 06, the time point of num is also added to the array as 0
Please give some guidance from experts! Thank you, brother!

First of all, you need to determine whether you need to insert relevant data and the plan of the data you insert. If you are not sure about the beginning and end of the missing shijian field, you need to traverse the array to determine it, and then add the missing fields, and then Use usort to sort.
You can also determine the position of your insertion when inserting, but since your insertion will affect the subscript of your array, the first method above is more convenient.
Reference Code

<code class="PHP"><?php


$max_shijian = date("H");

// 填充主方法
$fill_date = function ($input,$key) use($max_shijian) {
  $hours = range(0,$max_shijian);
  $riqi = null;
  // 筛选出不存在的时间
  // http://php.net/manual/zh/function.array-map.php
  array_map(function(&$item,$key) use(&$hours,&$riqi){
    empty($riqi) and $riqi = $item['riqi'];
    unset($hours[intval($item['shijian'])]);
  },$input);
  // 填充不存在的时间
  foreach ($hours as $hour) {
    $input[] =   [
        'key' => $key,
        'riqi'  => $riqi,
        'shijian' => getFullHour($hour),
        'num' => 0,
      ];
  }
  // 排序
  // http://php.net/manual/zh/function.usort.php
  usort($input,function($a,$b){
    return (intval($a['shijian'])<intval($b['shijian']))?-1:1;
  });
  return $input;
};

// 将小时补全为2位
function getFullHour($hour){
  return (strlen($hour)==1)?("0".$hour):$hour;
}

$json = '{"site001":[{"key":"site001","riqi":"2016-06-14","shijian":"00","num":"10"},{"key":"site001","riqi":"2016-06-14","shijian":"01","num":"4"},{"key":"site001","riqi":"2016-06-14","shijian":"02","num":"1"},{"key":"site001","riqi":"2016-06-14","shijian":"03","num":"3"},{"key":"site001","riqi":"2016-06-14","shijian":"04","num":"2"},{"key":"site001","riqi":"2016-06-14","shijian":"05","num":"1"},{"key":"site001","riqi":"2016-06-14","shijian":"07","num":"9"},{"key":"site001","riqi":"2016-06-14","shijian":"08","num":"2"}],"site002":[{"key":"site002","riqi":"2016-06-14","shijian":"00","num":"3"},{"key":"site002","riqi":"2016-06-14","shijian":"01","num":"13"},{"key":"site002","riqi":"2016-06-14","shijian":"02","num":"8"},{"key":"site002","riqi":"2016-06-14","shijian":"03","num":"23"},{"key":"site002","riqi":"2016-06-14","shijian":"04","num":"14"},{"key":"site002","riqi":"2016-06-14","shijian":"05","num":"6"},{"key":"site002","riqi":"2016-06-14","shijian":"06","num":"4"},{"key":"site002","riqi":"2016-06-14","shijian":"07","num":"7"},{"key":"site002","riqi":"2016-06-14","shijian":"08","num":"18"},{"key":"site002","riqi":"2016-06-14","shijian":"09","num":"6"}],"site003":[{"key":"site003","riqi":"2016-06-14","shijian":"00","num":"1"},{"key":"site003","riqi":"2016-06-14","shijian":"04","num":"2"},{"key":"site003","riqi":"2016-06-14","shijian":"07","num":"6"}],"site004":[{"key":"site004","riqi":"2016-06-14","shijian":"00","num":"3"},{"key":"site004","riqi":"2016-06-14","shijian":"02","num":"1"},{"key":"site004","riqi":"2016-06-14","shijian":"03","num":"4"},{"key":"site004","riqi":"2016-06-14","shijian":"04","num":"7"},{"key":"site004","riqi":"2016-06-14","shijian":"05","num":"4"},{"key":"site004","riqi":"2016-06-14","shijian":"06","num":"2"}]}';
$data = json_decode($json,true);

foreach ($data as $key => $value) {
  $data[$key] = call_user_func($fill_date,$value,$key);
}
print_r($data);


?></code>

array_map
usort

Let me talk about the idea I understand. First, you must first take the maximum value of the shijian field in your array, and then fill it in a loop based on the maximum value.

You try the following method,
The main ideas are:
1. First filter out the largest shijian value from the given array, and store the existing time value at the same time,
2. Add data that is less than the maximum shijian value and does not exist in the array

<code>    $srcArray = array
    (array(
                'key'  => 'site003',
                'riqi'=> '2016-06-14',
                'shijian' => 00,
                'num' => 1,
            )
                 ,array
            (
                'key' => 'site003',
                'riqi' => '2016-06-14',
                'shijian' => 02,
                'num'=> 2
            )
     ,array
            (
                'key' => 'site003',
                'riqi' => '2016-06-14',
                'shijian' => 04,
                'num'=> 2
            )
    );

    $data=array(-1);
    foreach($srcArray as $key=>$innerArray){
        array_push($data,$innerArray['shijian']);
        if($innerArray['shijian'] > $data[0]){
            $data[0] = intval($innerArray['shijian']);
        }
    }
    print_r($data);
    for($index = 0;$index<$data[0];$index++){
        $exits= !array_search(intval($index),$data);
        if($exits){
            $temp = array(
                'key'  => 'site003',
                'riqi'=> '2016-06-14',
                'shijian' => $index<10?'0'.$index:$index,
                'num' => 0,
            );
            array_push($srcArray,$temp);
        }
    }
    print_r($srcArray);</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn