基于相同的键合并两个不同的数组

时间:2017-02-22 09:33:56

标签: php merge

我有2个不同的数组

首先是

(int) 0 => [
        'id' => (int) 1,
        'file_name' => 'test',
        'imported_by' => 'John',
        'valid_to' => (float) 1767225600000
    ],
    (int) 1 => [
        'id' => (int) 2,
        'file_name' => null,
        'imported_by' => 'John',
        'valid_to' => (float) 1767225600000
    ],

第二个是

(int) 0 => object(App\Model\Entity\Product) {
        'id' => (int) 1,
        'product_cat' => (int) 10,
        'product_type' => (int) 1,
        'average....

我必须根据相同的键将第一个推入第二个

它看起来像这个

(int) 0 => object(App\Model\Entity\Product) {
        'id' => (int) 1,
        'product_category_id' => (int) 10,
        'product_vessel_id' => (int) 1,
        'average' => test;
        //content from first array
        'file_name' => 'test',
        'imported_by' => 'John',
        'valid_to' => (float) 1767225600000

我试过了 array_merge_recursive($ firstArray,$ secondArray);但这意味着在firstArray

的末尾添加元素

3 个答案:

答案 0 :(得分:1)

'use strict';

module.exports = function (grunt) {
    grunt.initConfig({
        exec: {
            protractorRunAppsTest: {
                cmd: 'C:\\Program Files\\nodejs\\node.exe C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\protractor\\built\\cli.js C:\\Jenkins\\workspace\\test\\conf.js'
                }
        },
        server: {
            port:3000,
            base: ['app']
        },
    });

    grunt.loadNpmTasks('grunt-contrib-connect'); 
    grunt.loadNpmTasks('grunt-protractor-runner');
    grunt.loadNpmTasks('grunt-run');
    grunt.loadNpmTasks('grunt-exec');

    grunt.registerTask('server', 'Start node server', function() {
        grunt.log.writeln('Started server on port 3000');
        require('./app.js');
    });
    grunt.registerTask('runAppsTest', ['exec:protractorRunAppsTest']);

};

答案 1 :(得分:0)

    <?php
$result = array();
$array_one=array(0 => array('id' =>  1,
        'file_name' => 'test',
        'imported_by' => 'John',
        'valid_to' => 1767225600000),

    1 => array(
        'id' =>  2,
        'file_name' => "",
        'imported_by' => 'John',
        'valid_to' => 1767225600000)



        );


$array_seond=   array(
                    0 => array(
                                'id' =>  1,
                        'product_cat' =>  10,
                        'product_type' =>  1,
                        'average' => 'test'
                    )
                );  


    echo "<pre>";




//print_r($result);

    foreach($array_seond as $key => $value){
                foreach($array_one as $value_second ){

                    if($value['id']==$value_second['id']){
                        $result[$key] =array_merge($array_seond[$key],$value_second);

                    }

                }

    }


    print_r($result);
?>

答案 2 :(得分:0)

您可以使用此功能:

function merge_two_arrays($array1,$array2) {
        $data = array();
        $arrayAB = array_merge($array1,$array2);
        foreach ($arrayAB as $value) {
          $id = $value['id'];
          if (!isset($data[$id])) {
            $data[$id] = array();
          }
          $data[$id] = array_merge($data[$id],$value);
        }
        return $data;
      }

    $master_array = merge_two_arrays($array1,$array2);