如何减少数组值中的一部分?

发布于 2024-11-28 02:34:58 字数 411 浏览 1 评论 0原文

我有一个格式为

array() {["2011-07-29"]=> 39 ["2011-07-30"]=> 39 ["2011-07-31"]=> 39 ["2011-08-01"]=> 40}

我需要 t0 将中间键值递减 1 的数组,即 ["2011-07-29"] 到 ["2011-06-29"]

输出应该

array() {["2011-06-29"]=> 39 ["2011-06-30"]=> 39 ["2011-06-31"]=> 39 ["2011-07-01"]=> 40}

怎么做?

i have an array in the format

array() {["2011-07-29"]=> 39 ["2011-07-30"]=> 39 ["2011-07-31"]=> 39 ["2011-08-01"]=> 40}

i need t0 decrement the middle keyvalue by 1 i.e ["2011-07-29"] to ["2011-06-29"]

the output should be

array() {["2011-06-29"]=> 39 ["2011-06-30"]=> 39 ["2011-06-31"]=> 39 ["2011-07-01"]=> 40}

how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

带刺的爱情 2024-12-05 02:34:58

就像 Fernaref 解释的那样:通过解析键的值来根据您的需要更改键。有多种方法可以实现这一目标,这只是一个示例(演示):

<?php

$data = array(
  '2011-07-29' => 39,
  '2011-07-30' => 39,
  '2011-07-31' => 39,
  '2011-08-01' => 40,
);

$keys = array_keys($data);

foreach($keys as &$key)
{
    list(,$month) = sscanf($key, '%d-%d-%d');
    $month = sprintf("%02d", $month-1);
    $key[5] = $month[0];
    $key[6] = $month[1];
}
unset($key);

$data = array_combine($keys, $data);

print_r($data);

Like Fernaref explained: Change the keys according to your needs by parsing their value. There are multiple ways to accomplish that, this is just one example (Demo):

<?php

$data = array(
  '2011-07-29' => 39,
  '2011-07-30' => 39,
  '2011-07-31' => 39,
  '2011-08-01' => 40,
);

$keys = array_keys($data);

foreach($keys as &$key)
{
    list(,$month) = sscanf($key, '%d-%d-%d');
    $month = sprintf("%02d", $month-1);
    $key[5] = $month[0];
    $key[6] = $month[1];
}
unset($key);

$data = array_combine($keys, $data);

print_r($data);
眼波传意 2024-12-05 02:34:58

它是一个字符串 - 您必须解析数据,递减值并将密钥重新组合在一起。或者首先使用更好的密钥。

It's a string - you'll have to parse the data out, decrement the value and put the key back together. Or use a better key in the first place.

零度℉ 2024-12-05 02:34:58

试试这个:

$result = array();
foreach ($array as $key => $val) {
  $date = strtotime ($key);

  $result[date("Y-m-d", strtotime("- month", $date)] = $val;
}

try this:

$result = array();
foreach ($array as $key => $val) {
  $date = strtotime ($key);

  $result[date("Y-m-d", strtotime("- month", $date)] = $val;
}
2024-12-05 02:34:58
$input = array("2011-07-29"=>39, "2011-07-30"=>39, "2011-07-31"=>39, "2011-08-01"=>40);
$output = array();

foreach($input as $key => $value) {
    $key = preg_replace_callback("/(\d{4})-(\d{2})-/", function($match) {
        $match[2] = (int) $match[2] - 1;
        if( $match[2] < 1 ) { // don't forget to decrement the year, if the month goes below 1
            $match[1] = (int) $match[1] - 1;
            $match[2] = 12;
        }
        return $match[1] . "-" . str_pad($match[2], 2, "0", STR_PAD_LEFT) . "-";
    }, $key);
    $output[$key] = $value;
}

print_r($output);
$input = array("2011-07-29"=>39, "2011-07-30"=>39, "2011-07-31"=>39, "2011-08-01"=>40);
$output = array();

foreach($input as $key => $value) {
    $key = preg_replace_callback("/(\d{4})-(\d{2})-/", function($match) {
        $match[2] = (int) $match[2] - 1;
        if( $match[2] < 1 ) { // don't forget to decrement the year, if the month goes below 1
            $match[1] = (int) $match[1] - 1;
            $match[2] = 12;
        }
        return $match[1] . "-" . str_pad($match[2], 2, "0", STR_PAD_LEFT) . "-";
    }, $key);
    $output[$key] = $value;
}

print_r($output);
因为看清所以看轻 2024-12-05 02:34:58

某些功能可以是

preg_replace("@(\d\d\d\d)-(\d\d)@e","'\$1-'.str_pad($2-1, 2, '0', STR_PAD_LEFT)",$key);

somefunction can be

preg_replace("@(\d\d\d\d)-(\d\d)@e","'\$1-'.str_pad($2-1, 2, '0', STR_PAD_LEFT)",$key);
拥有 2024-12-05 02:34:58
$newArray = array();
foreach ($array as $key => $value)
{
  $newKey = someFunction($key);
  $newArray[$newKey] = $value;
}

而“someFunction”将转换您的日期以创建新密钥

$newArray = array();
foreach ($array as $key => $value)
{
  $newKey = someFunction($key);
  $newArray[$newKey] = $value;
}

While "someFunction" will convert your date to create the new key

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文