使用给定数组示例中的列值进行数组合并

时间:2018-04-19 13:11:04

标签: php

我想用年份添加两个数组列 第一个数组

04

第二个数组

@echo off
setlocal EnableDelayedExpansion

rem Read all lines, excepting the first one:
set "string="
for /F "skip=1" %%a in (input.txt) do set "string=!string!%%a"

rem Remove the first element:
set "string=%string:*:=%"

rem Show the rest, removing colons:
echo %string::=%

)``

在这里输入代码

输出如下所示。

Array
(
[0] => Array
    (
        [year] => 2016
        [voucher] => 7
    )

[1] => Array
    (
        [year] => 2017
        [voucher] => 9
    )

[2] => Array
    (
        [year] => 2018
        [voucher] => 6
    )

[3] => Array
    (
        [year] => 2015
        [voucher] => 1
    )

[4] => Array
    (
        [year] => 2014
        [voucher] => 2
    )

1 个答案:

答案 0 :(得分:0)

我是这样做的:

$arA = array( '0' => array( 'year' => '2016', 'voucher' => '7' ),
              '1' => array( 'year' => '2017', 'voucher' => '9' ),
              '2' => array( 'year' => '2018', 'voucher' => '6' ),
              '3' => array( 'year' => '2015', 'voucher' => '1' ),
              '4' => array( 'year' => '2014', 'voucher' => '2' )

          );


$arB = array( '0' => array( 'year' => '2018', 'sample' => '4' ),
              '1' => array( 'year' => '2017', 'sample' => '3' ),
              '2' => array( 'year' => '2016', 'sample' => '2' ),
              '3' => array( 'year' => '2015', 'sample' => '1' )

          );


$newArr = array_merge( $arA, $arB );

$output = array();

foreach( $newArr as $v ) {

    if( !isset( $output[ $v[ "year" ] ] ) ) {

        $output[ $v[ "year" ] ] = $v;

    }else {

        $output[ $v[ "year" ] ][ "sample" ] = $v[ "sample" ];

    }

}

$output = array_values( $output );

print_r( $output );

如果您找到更好的解决方案,请告诉我。