数组中的值和简化键名称

时间:2014-08-08 07:25:48

标签: php arrays

这与我正在处理的同一项目有关的其他问题几乎相似.. Link to my other question
但在这种情况下,数组不同如下:

Array
(
    [2014-08-01 11:27:03] => 2
    [2014-08-01 11:52:57] => 2
    [2014-08-01 11:54:49] => 2
    [2014-08-02 11:59:54] => 4
    [2014-08-02 12:02:41] => 2
    [2014-08-05 12:09:38] => 4
    [2014-08-07 12:23:12] => 3
    [2014-08-07 12:25:18] => 3
// and so on...
)

那是我的输出数组,为了得到那个数组,我不得不做一些奇迹......无论如何,基于那个数组,我必须总结每个关键日期的值,并构建一个像这样的数组。

Array
(
    [2014-08-01] => 6
    [2014-08-02] => 6
    [2014-08-05] => 4
    [2014-08-07] => 6
// and so on...
)

最后一个数组将用于使用morrisonJS构建图形,我所拥有的是:

$res_meno = array();
foreach ($sunArr as $keys => $values) {
    $arrays= explode(" ",$sumArr[$keys]);
    $res_meno[] = $arrays[0];
}
$vals_char2 = array_count_values($res_meno); 

这是我尝试构建我的最后一个阵列但是没有工作......
任何帮助将不胜感激!

感谢您抽出宝贵时间。

5 个答案:

答案 0 :(得分:1)

试试此代码

<?php 
$arr = array(
 "2014-08-01 11:27:03" => 2,
 "2014-08-01 11:52:57" => 2,
 "2014-08-01 11:54:49" => 2,
 "2014-08-02 11:59:54" => 4,
 "2014-08-02 12:02:41" => 2,
 "2014-08-05 12:09:38" => 4,
 "2014-08-07 12:23:12" => 3,
 "2014-08-07 12:25:18" => 3
);

$new_array = array();
foreach($arr as $k => $v){
   $date = reset(explode(" ", $k));
   if(isset($new_array[$date])){
      $new_array[$date] += $v;
   }
   else{
     $new_array[$date] = $v;
   }
}
print_r($new_array);

?>

DEMO

答案 1 :(得分:1)

  $sunArr = array
(
    "2014-08-01 11:27:03" => 2,
    "2014-08-01 11:52:57" => 2,
    "2014-08-01 11:54:49" => 2,
    "2014-08-02 11:59:54" => 4,
    "2014-08-02 12:02:41" => 2,
    "2014-08-05 12:09:38" => 4,
    "2014-08-07 12:23:12" => 3,
    "2014-08-07 12:25:18" => 3,
);

  $res_meno = array();

    foreach ($sunArr as $keys => $values) {

        $arrays= explode(" ",$keys);

        if(isset($res_meno[$arrays[0]]))
        {
           $res_meno[$arrays[0]] = $res_meno[$arrays[0]] + $values;
        }
        else
        {
           $res_meno[$arrays[0]] = $values;
        }
    }

    print_r($res_meno);
    exit;

试试这个,我认为它可能会解决问题

答案 2 :(得分:0)

第一个阵列来自哪里?如果它来自SQL数据库,最好创建一个返回聚合数组的查询。

否则不需要使用array_key_values:

$res_meno = array();
foreach ($sumArr as $keys => $values) {
    $key = substr($keys, 0, 10);
    $res_meno[$key] = (empty($res_meno[$key]) ? 0 : $res_meno[$key]) + $values;
}

答案 3 :(得分:0)

试试这个PHP代码您可以测试here

    $sunArr = Array
    (
        '2014-08-01 11:27:03' => 2,
        '2014-08-01 11:52:57' => 2,
        '2014-08-01 11:54:49' => 2,
        '2014-08-02 11:59:54' => 4,
        '2014-08-02 12:02:41' => 2,
        '2014-08-05 12:09:3' => 4,
        '2014-08-07 12:23:12' => 3,
        '2014-08-07 12:25:18' => 3

    );

    $key = 0;
    $res_meno = array();
    foreach ($sunArr as $keys => $values)
    {
        $ar= explode(" ", $keys);

        if( $key == $ar[0] )
        {       
            $res_meno[$key] = $sunArr[$keys] + $res_meno[$key];
        }
        else
        {
            $key = $ar[0];
            $res_meno[$key] = $values;      
        }  
    }

    echo '<pre>';
    print_r($res_meno);
    die;

答案 4 :(得分:0)

这是一个使用回调的解决方案。 不使用循环通常更好!

$sunArr = array(
    '2014-08-01 11:27:03' => 3,
    '2014-08-01 11:27:05' => 5,
    '2013-09-01 11:01:05' => 1
);

$res = array();
function map($item, $key, &$result)
{
    $result[current(explode(" ", $key))] += $item;
}

array_walk($sunArr, "map", &$res);
var_dump($res);

您可以对其进行测试here on codepad