如何获得将第二个数组的元素作为参数对一个数组的所有元素进行操作的结果数组? - php

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center。

7年前关闭。

好的,所以这很容易。我有两个数组。数组一的结构如下:

[
    [                                        
        'value' => 3337
        'end_time' => '2012-10-07T07:00:00+0000'
    ],
    [
        'value' => 2811
        'end_time' => '2012-10-09T07:00:00+0000'
    ],
    [                                        
        'value' => 1318
        'end_time' => '2012-10-14T07:00:00+0000'
    ]

和数组2:

[  
    [
        '_id' => '2012-10-07'
        'value' => 50
    ],
    [
        '_id' => '2012-10-09'
        'value' => 15
    ],
    [
        '_id' => '2012-10-10'
        'value' => 2
    ]
]

所以...。我想基于array1 [_id] = substr(array2 ['end_time'],0,10)将数组1的元素除以数组2的元素。

数组2没有array1中所有ID的结束时间值。

结果数组应类似于以下内容:

[
    '2012-10-01' => 0.11
    '2012-10-02' => 0 (if this date is not available in the second array). 
    '2012-10-03' => 0.12312
]

这样做:

$kermit = array();
foreach($dauresult['data']['0']['values'] as $subdau) {  
    foreach($revenue['results'] as $subrev) {
          $date = substr($subdau['end_time'], 0, 10);
          if($date == $subrev['_id']) {
                $kermit[$date] = $subrev['value']/$subdau['value']; 
               } 
           }
    }

我可以得到一个看起来像这样的数组:

Array
(
    [2012-09-30] => 0.0044950554390171
    [2012-10-01] => 0.019565990750623
    [2012-10-05] => 0.015487397509869
    [2012-10-07] => 0.020177562550444
    [2012-10-09] => 0.0075150300601202
    [2012-10-10] => 0.00095831336847149
    [2012-10-11] => 0.0010183299389002
    [2012-10-12] => 0.0010126582278481
    [2012-10-13] => 0.029866666666667
    [2012-10-14] => 0.029779630732579
    [2012-10-15] => 0.011926058437686
    [2012-10-18] => 0.0018844221105528
    [2012-10-19] => 0.0005941770647653
    [2012-10-21] => 0.0023781212841855
    [2012-10-27] => 0.0011820330969267
    [2012-10-28] => 0.0011467889908257
)

但是我不能做的是让缺失的日期(10-20、10-16等)显示为0。愚蠢的问题,但是让我有些疯狂。有什么建议?
谢谢!
-蕨类

参考方案

可以通过使用查询范围初始化数组来解决此问题。

$from = strtotime('2012-09-30'); $to = strtotime('2012-10-28');
for ($i = $from; $i < $to; $i += 86400) {
    $a[date('Y-m-d', $i)] = 0;
}

并非总是最正确的解决方案,但它应该为您指明正确的方向。

PHP:对数组排序 - php

请如何排序以下数组Array ( 'ben' => 1.0, 'ken' => 2.0, 'sam' => 1.5 ) 至Array ( 'ken' => 2.0, 'sam' => 1.5, 'ben' =&…

PHP PDO组按列名称查询结果 - php

以下PDO查询返回以下结果:$db = new PDO('....'); $sth = $db->prepare('SELECT ...'); 结果如下: name curso ABC stack CDE stack FGH stack IJK stack LMN overflow OPQ overflow RS…

PHP-全局变量的性能和内存问题 - php

假设情况:我在php中运行一个复杂的站点,并且我使用了很多全局变量。我可以将变量存储在现有的全局范围内,例如$_REQUEST['userInfo'],$_REQUEST['foo']和$_REQUEST['bar']等,然后将许多不同的内容放入请求范围内(这将是适当的用法,因为这些数据指的是要求自…

PHP strtotime困境 - php

有人可以解释为什么这在我的服务器上输出为true吗?date_default_timezone_set('Europe/Bucharest'); var_dump( strtotime('29.03.2015 03:00', time()) === strtotime('29.03.2015 04:00�…

php-casperjs获取内部文本 - php

我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…