如何使用第二个数组的元素作为参数对数组的所有元素应用操作,从而得到结果数组?

时间:2021-05-21 15:07:01

Okay- so this one should be easy. I have two arrays. Array one has an structure like this:

这个应该很简单。我有两个数组。数组的结构是这样的:

[
    [                                        
        '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'
    ]

And array 2:

和数组2:

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

SO....I want to divide the elements of array one by the elements of array 2 based on array1[_id] = substr(array2['end_time'], 0, 10).

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

Array 2 does not have a end time value for all the id's in array1.

数组2没有array1中所有id的最终时间值。

The resulting array should look similar to this:

得到的数组应该类似如下:

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

Doing this:

这样做:

$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']; 
               } 
           }
    }

I can get an array that looks like this:

我可以得到一个这样的数组:

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
)

But what I can't do is get the missing dates (10-20, 10-16, etc) to show up with 0. Dumb problem but it's driving me a little crazy. Any suggestions? Thanks! -Fern

但是我不能做的是让丢失的日期(10-20、10-16等等)以0出现。愚蠢的问题,但这让我有点疯狂。有什么建议吗?谢谢!蕨类植物

3 个解决方案

#1


2  

This problem can be solved by initializing the array with the range you're querying on.

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

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

Not always the most correct solution, but it should point you in the right direction.

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

#2


2  

The way I see it, you want that the dates in array 1 not appearing in array 2 be included in the final array. To do that, you can do a second loop after finishing the loop you wrote, that simply adds the dates missing from your first loop. That second loop would go something like this:

按照我的理解,您希望数组1中没有出现在数组2中的日期包含在最终的数组中。为此,您可以在完成编写的循环之后执行第二个循环,这只需添加第一个循环中丢失的日期。第二个循环会是这样的

foreach($dauresult['data']['0']['values'] as $subdau) {  
    $date = substr($subdau['end_time'], 0, 10);
    if(!array_key_exists($date, $kermit))
        $kermit[$date] = 0;
}

I believe that you have to do it in two-passes since you have no way of knowing in the first pass whether all dates from the first are in the second. I could be wrong though.

我相信你必须在两轮之内完成,因为你无法知道第一轮是否所有第一轮的日期都在第二轮。但我可能错了。

#3


2  

I would use array_map on the first array.

我会在第一个数组中使用array_map。

The callback function would return 0 if the _id didn't exist in the second array and it'd return the result of the calculation if it did:

如果第二个数组中不存在_id,回调函数将返回0,如果存在,则返回计算结果:

//example arrays:
$firstArray = 
[
    [
        'value' => 3337,
        'end_time' => '2012-09-30T07:00:00+0000'
    ],
    [
        'value' => 2811,
        'end_time' => '2012-10-01T07:00:00+0000'
    ]
];

$secondArray = 
[
    [
        '_id' => '2012-09-30',
        'value' => 15
    ]
];

$resultingArray = array_map
(
    function($elementFromFirstArray) use ($secondArray)
    {
        foreach($secondArray as $elementFromSecondArray)
        {
            if($elementFromSecondArray['_id'] == substr($elementFromFirstArray['end_time'], 0, 10))
            {
                return $elementFromFirstArray['value']/$elementFromSecondArray['value'];
            }
        }
        return 0;
    }, 
    $firstArray
);

#1


2  

This problem can be solved by initializing the array with the range you're querying on.

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

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

Not always the most correct solution, but it should point you in the right direction.

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

#2


2  

The way I see it, you want that the dates in array 1 not appearing in array 2 be included in the final array. To do that, you can do a second loop after finishing the loop you wrote, that simply adds the dates missing from your first loop. That second loop would go something like this:

按照我的理解,您希望数组1中没有出现在数组2中的日期包含在最终的数组中。为此,您可以在完成编写的循环之后执行第二个循环,这只需添加第一个循环中丢失的日期。第二个循环会是这样的

foreach($dauresult['data']['0']['values'] as $subdau) {  
    $date = substr($subdau['end_time'], 0, 10);
    if(!array_key_exists($date, $kermit))
        $kermit[$date] = 0;
}

I believe that you have to do it in two-passes since you have no way of knowing in the first pass whether all dates from the first are in the second. I could be wrong though.

我相信你必须在两轮之内完成,因为你无法知道第一轮是否所有第一轮的日期都在第二轮。但我可能错了。

#3


2  

I would use array_map on the first array.

我会在第一个数组中使用array_map。

The callback function would return 0 if the _id didn't exist in the second array and it'd return the result of the calculation if it did:

如果第二个数组中不存在_id,回调函数将返回0,如果存在,则返回计算结果:

//example arrays:
$firstArray = 
[
    [
        'value' => 3337,
        'end_time' => '2012-09-30T07:00:00+0000'
    ],
    [
        'value' => 2811,
        'end_time' => '2012-10-01T07:00:00+0000'
    ]
];

$secondArray = 
[
    [
        '_id' => '2012-09-30',
        'value' => 15
    ]
];

$resultingArray = array_map
(
    function($elementFromFirstArray) use ($secondArray)
    {
        foreach($secondArray as $elementFromSecondArray)
        {
            if($elementFromSecondArray['_id'] == substr($elementFromFirstArray['end_time'], 0, 10))
            {
                return $elementFromFirstArray['value']/$elementFromSecondArray['value'];
            }
        }
        return 0;
    }, 
    $firstArray
);