How does PHP add data to the interval array?-php Tutorial

Source: Internet
Author: User
An existing Array is as follows: [site003] & amp; gt; Array {code ...} because the time shijian field 00 to-04 does not have 01,02, 03, so I want to add 0 to the num field of the data assembled at 01,02 and 03, the latter is also supplemented by the num time point at the intermediate time point, 06... an array is as follows:
[Site003] => Array

    (        [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            )    )

Because the time shijian field 00 to-04 does not have 01,02, 03, so I want to add 0 to the num field of the data assembled at 01,02 and 03,
The latter intermediate time point also adds an array of 0 to the num time point
Please give me some advice! Thank you!

Reply content:

An array is as follows:
[Site003] => Array

    (        [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            )    )

Because the time shijian field 00 to-04 does not have 01,02, 03, so I want to add 0 to the num field of the data assembled at 01,02 and 03,
The latter intermediate time point also adds an array of 0 to the num time point
Please give me some advice! Thank you!

First, you need to determine whether you need to insert the relevant data and the scheme of the data you have inserted. if you cannot determine the beginning or end of the missing shijian field in the middle, you need to traverse this array to determine, then add the fields that do not exist and use usort for sorting.
You can also determine the position of your insert when inserting, but because your insert will affect the subscript of your array, it is more convenient to use the first method above.
Reference Code


  $ Key, 'riqi' => $ riqi, 'shijian '=> getFullHour ($ hour), 'num' => 0,];} // sort // http://php.net/manual/zh/function.usort.php usort ($ input, function ($ a, $ B) {return (intval ($ a ['shijian '])
  
   
$ Value) {$ data [$ key] = call_user_func ($ fill_date, $ value, $ key);} print_r ($ data);?>
  

Array_map
Usort

Let me talk about my understanding. First, we need to take your arrayshijianThe maximum value of the field, and then cyclically fill according to the maximum value.

Try the following methods,
The main idea is:
1. filter out the maximum shijian value from the given array and store the existing time value,
2. add data in the array that is smaller than the maximum shijian value and does not exist

    $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);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.