如何从数组创建唯一的数组

时间:2017-10-02 07:03:51

标签: php arrays array-unique

阵列

[0] => Array
    (
        [a1] => 12
        [v1] => 3100.00
        [v2] => 186.00
        [v3] => 186.00
    )

[1] => Array
    (
        [a1] => 12
        [v1] => 1200.00
        [v2] => 72.00
        [v3] => 72.00
    )

我想从这个数组创建新数组 这看起来像下面给出的那样它应该给我'12'普通并添加其他值

阵列

[0] => Array
    (
        [a1] => 12
        [v1] => 4300.00
        [v2] => 258.00
        [v3] => 258.00
    )

4 个答案:

答案 0 :(得分:1)

'use strict';

var Alexa = require('alexa-sdk');
var request = require('request');
var constants = require('./constants');

exports.handler = function(event, context, callback){
    var alexa = Alexa.handler(event, context, callback);
    alexa.registerHandlers(stateHandlers.startModeIntentHandlers);
    alexa.execute();
};

var stateHandlers = {
    startModeIntentHandlers : Alexa.CreateStateHandler(constants.states.START_MODE, {
        "LaunchRequest": function () {
            this.emit(':ask', 'Welcome to Douban FM');
        },

        "Play": function() {
            request.get(url.playlistUrl, function (error, response, body) {
                console.log('error:', error);
                console.log('statusCode:', response && response.statusCode);
                console.log('body:', body);
                // var d = JSON.parse(data);
                // var result = d.song.get(0).url;
                // console.log('result url:', result);
            });
            this.emit(':tell', "Ok.");
        }
    })
};

var url = {
    playlistUrl : 'https://douban.fm/j/mine/playlist?type=n&sid=966135&pt=206.8&channel=1&pb=64&from=mainsite&r=1d56c92354'
};

答案 1 :(得分:0)

请在下面找到代码:

<?php
        $merged = array();
        $res =  array('a1'=>12,'v1'=>3100.00,'v2'=>186.00,'v3'=>186.00);
        $res1 = array('a1'=>12,'v1'=>1200.00,'v2'=>72.00,'v3'=>72.00);

        foreach ([$res, $res1] as $a) {                             // iterate both arrays
            foreach ($a as $key => $value) {                     // iterate all keys+values
                $merged[$key] = $value + (isset($merged[$key]) ? $merged[$key] : 0);   // merge and add
            }
        }

    print "<pre>";
    print_r($merged); 
    die;
    ?>

答案 2 :(得分:0)

有点不清楚真正的需求,所以这个解决方案只是少数可能的解决方案之一。

此脚本a1元素用作键,因此它可以使用多个a1值。

示例:

<?php
$a = [
    [
        'a1' => 12,
        'v1' => 3100.00,
        'v2' => 186.00,
        'v3' => 186.00,
    ],
    [
        'a1' => 12,
        'v1' => 1200.00,
        'v2' => 72.00,
        'v3' => 72.00,
    ],
    [
        'a1' => 13,
        'v1' => 2100.00,
        'v2' => 386.00,
        'v3' => 386.00,
    ],
    [
        'a1' => 13,
        'v1' => 1200.00,
        'v2' => 72.00,
        'v3' => 72.00,
    ]
];

$r = [];
foreach ($a as $item) {
    $key = $item['a1'];
    if (empty($r[$key]))
        $r[$key] = $item;
    else {
        foreach ($item as $k => $v) {
            if ($k !== 'a1')
                $r[$key][$k] = empty($r[$key][$k]) ? $item[$k] : $r[$key][$k] + $item[$k];
        }
    }
}

print_r(array_values($r));

答案 3 :(得分:-1)

一个简单的foreach循环可以做到。

$result = array();
foreach($yourArray as $arr){
    foreach($arr as $i=>$v) {
        if(!isset($result[$i])) {
            $result[$i] = 0;
        }
        if($i == 'a1'){ 
            $result[$i] = $v;
        } else {
            $result[$i] += $v;
        }
    }
}