Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按周排列阵列_Php_Arrays_Date - Fatal编程技术网

Php 按周排列阵列

Php 按周排列阵列,php,arrays,date,Php,Arrays,Date,我有一个数组,其中有一个名为date的字段,我需要的是将所有这些数组分为几个星期。有可能这样做吗?这是我的密码: function getWeeks($query){ $postdate = $query['response']['posts']['date']; return $posts; } 以下是我的阵列的一部分: Array ( [date] => 07/30/12 [message] => test [post_id] => 1 [impres

我有一个数组,其中有一个名为date的字段,我需要的是将所有这些数组分为几个星期。有可能这样做吗?这是我的密码:

function getWeeks($query){
    $postdate = $query['response']['posts']['date'];


    return $posts;
}
以下是我的阵列的一部分:

Array ( [date] => 07/30/12 [message] => test [post_id] => 1 [impressions] => Array ( [0] => 9638 ) [consumptions] => Array ( [0] => 38 ) [storytellers] => Array ( [0] => 6 ) [engaged_users] => Array ( [0] => 31 ) [story_adds] => Array ( [0] => 6 ) [impressions_unique] => Array ( [0] => 4700 ) [comment] => Array ( [0] => 1 ) [like] => Array ( [0] => 5 ) [share] => Array ( [0] => 0 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) )

Array ( [date] => 07/30/12 [message] => test2 [post_id] => 2 [impressions] => Array ( [0] => 10552 ) [consumptions] => Array ( [0] => 47 ) [storytellers] => Array ( [0] => 5 ) [engaged_users] => Array ( [0] => 44 ) [story_adds] => Array ( [0] => 5 ) [impressions_unique] => Array ( [0] => 4982 ) [comment] => Array ( [0] => 0 ) [like] => Array ( [0] => 4 ) [share] => Array ( [0] => 1 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) ) 

这将循环浏览每个帖子,如果它们在同一周,则将它们分组


这将循环浏览每个帖子,如果它们在同一周,则将它们分组


日期'W',STROTIME'07/30/12'可能对您有所帮助。日期'W',STROTIME'07/30/12'可能对您有所帮助$周=日期'Y W',标准时间$行['0'];如果需要按周和年排序,因为现在2015 01 01和2016 01 01在同一个数组中:您好,是否可以为每个星期创建一个密钥?示例:[0]-2019-02-26[1]-2019-02-27$week=date'Y W',strottime$line['0'];如果需要按周和年排序,因为现在2015 01 01和2016 01 01在同一个数组中:您好,是否可以为每个星期创建一个密钥?示例:[0]-2019-02-26[1]-2019-02-27
<?php

$posts = array(
  array('date' => '7/30/10', 'title' => 'july post'),
  array('date' => '7/19/10', 'title' => 'another post in july'),
  array('date' => '7/22/10', 'title' => 'sup, this will be with another post')
);

$grouped_posts = array();

foreach( $posts as $post ) {
  // @see http://us2.php.net/manual/en/function.date.php
  $week = date('W', strtotime($post['date']));

  // create new empty array if it hasn't been created yet
  if( !isset($grouped_posts[$week]) ) {
    $grouped_posts[$week] = array();
  }

  // append the post to the array
  $grouped_posts[$week][] = $post;
}

print_r($grouped_posts);