php 创建具有额外值的第三个数组,其中的键与两个现有数组相匹配
php create a third array with an extra value where the keys match from two existing arrays
我有两个数组:
$graph1 = Array (
[0] => Array ( [0] => 202101 [1] => 2 )
[1] => Array ( [0] => 202102 [1] => 3 )
[2] => Array ( [0] => 202103 [1] => 5 )
[3] => Array ( [0] => 202104 [1] => 2 )
[4] => Array ( [0] => 202105 [1] => 3 )
[5] => Array ( [0] => 202106 [1] => 4 )
[6] => Array ( [0] => 202107 [1] => 14 ) )
$graph2 = Array (
[0] => Array ( [0] => 202105 [1] => 3 )
[1] => Array ( [0] => 202107 [1] => 1 )
[2] => Array ( [0] => 202108 [1] => 6 )
)
并想创建第三个数组,其中包含值[0]匹配的额外值,如果没有匹配值[0],则创建额外的行和额外的值
我试过 array_merge 和 array_push 但我对数组的理解无法胜任
我想要的输出是这样的:
$graph3 = Array (
[0] => Array ( [0] => 202101 [1] => 2 [2] => 0 )
[1] => Array ( [0] => 202102 [1] => 3 [2] => 0 )
[2] => Array ( [0] => 202103 [1] => 5 [2] => 0 )
[3] => Array ( [0] => 202104 [1] => 2 [2] => 0 )
[4] => Array ( [0] => 202105 [1] => 3 [2] => 3 )
[5] => Array ( [0] => 202106 [1] => 4 [2] => 0 )
[6] => Array ( [0] => 202107 [1] => 14 [2] => 1 )
[7] => Array ( [0] => 202108 [1] => 0 [2] => 6 ) )
答案和解释会很棒 - 谢谢
这个函数应该可以解决问题...
function combine_graphs( ...$arrays ) {
$output = [];
// First create keyed array
foreach ( $arrays as $row ) {
foreach ( $row as $columns ) {
$key = $columns[ 0 ];
if ( !array_key_exists( $key, $output ) ) {
$output[ $key ] = [ $key ];
}
}
}
// Now add values
foreach ( $output as $key => $values ) {
foreach ( $arrays as $row ) {
$index = array_search( $key, array_column( $row, 0 ) );
$output[ $key ][] = $index !== false ? $row[ $index ][ 1 ] : 0;
}
}
return array_values( $output );
}
用法是...
$graph1 = [
[ 202101, 2 ],
[ 202102, 3 ],
[ 202103, 5 ],
[ 202104, 2 ],
[ 202105, 3 ],
[ 202106, 4 ],
[ 202107, 14 ],
];
$graph2 = [
[ 202105, 3 ],
[ 202107, 1 ],
[ 202108, 6 ],
];
$graph3 = combine_graphs( $graph1, $graph2 );
该函数允许组合任意数量的图形,例如...
$graph1 = [
[ 202101, 2 ],
[ 202102, 3 ],
[ 202103, 5 ],
[ 202104, 2 ],
[ 202105, 3 ],
[ 202106, 4 ],
[ 202107, 14 ],
];
$graph2 = [
[ 202105, 3 ],
[ 202107, 1 ],
[ 202108, 6 ],
];
$graph3 = [
[ 202105, 3 ],
[ 202107, 4 ],
[ 202108, 9 ],
[ 202107, 12 ],
[ 202104, 2 ],
];
$graph4 = combine_graphs( $graph1, $graph2, $graph3 );
以上会输出...
Array
(
[0] => Array
(
[0] => 202101
[1] => 2
[2] => 0
[3] => 0
)
[1] => Array
(
[0] => 202102
[1] => 3
[2] => 0
[3] => 0
)
[2] => Array
(
[0] => 202103
[1] => 5
[2] => 0
[3] => 0
)
[3] => Array
(
[0] => 202104
[1] => 2
[2] => 0
[3] => 2
)
[4] => Array
(
[0] => 202105
[1] => 3
[2] => 3
[3] => 3
)
[5] => Array
(
[0] => 202106
[1] => 4
[2] => 0
[3] => 0
)
[6] => Array
(
[0] => 202107
[1] => 14
[2] => 1
[3] => 4
)
[7] => Array
(
[0] => 202108
[1] => 0
[2] => 6
[3] => 9
)
)
我有两个数组:
$graph1 = Array (
[0] => Array ( [0] => 202101 [1] => 2 )
[1] => Array ( [0] => 202102 [1] => 3 )
[2] => Array ( [0] => 202103 [1] => 5 )
[3] => Array ( [0] => 202104 [1] => 2 )
[4] => Array ( [0] => 202105 [1] => 3 )
[5] => Array ( [0] => 202106 [1] => 4 )
[6] => Array ( [0] => 202107 [1] => 14 ) )
$graph2 = Array (
[0] => Array ( [0] => 202105 [1] => 3 )
[1] => Array ( [0] => 202107 [1] => 1 )
[2] => Array ( [0] => 202108 [1] => 6 )
)
并想创建第三个数组,其中包含值[0]匹配的额外值,如果没有匹配值[0],则创建额外的行和额外的值
我试过 array_merge 和 array_push 但我对数组的理解无法胜任 我想要的输出是这样的:
$graph3 = Array (
[0] => Array ( [0] => 202101 [1] => 2 [2] => 0 )
[1] => Array ( [0] => 202102 [1] => 3 [2] => 0 )
[2] => Array ( [0] => 202103 [1] => 5 [2] => 0 )
[3] => Array ( [0] => 202104 [1] => 2 [2] => 0 )
[4] => Array ( [0] => 202105 [1] => 3 [2] => 3 )
[5] => Array ( [0] => 202106 [1] => 4 [2] => 0 )
[6] => Array ( [0] => 202107 [1] => 14 [2] => 1 )
[7] => Array ( [0] => 202108 [1] => 0 [2] => 6 ) )
答案和解释会很棒 - 谢谢
这个函数应该可以解决问题...
function combine_graphs( ...$arrays ) {
$output = [];
// First create keyed array
foreach ( $arrays as $row ) {
foreach ( $row as $columns ) {
$key = $columns[ 0 ];
if ( !array_key_exists( $key, $output ) ) {
$output[ $key ] = [ $key ];
}
}
}
// Now add values
foreach ( $output as $key => $values ) {
foreach ( $arrays as $row ) {
$index = array_search( $key, array_column( $row, 0 ) );
$output[ $key ][] = $index !== false ? $row[ $index ][ 1 ] : 0;
}
}
return array_values( $output );
}
用法是...
$graph1 = [
[ 202101, 2 ],
[ 202102, 3 ],
[ 202103, 5 ],
[ 202104, 2 ],
[ 202105, 3 ],
[ 202106, 4 ],
[ 202107, 14 ],
];
$graph2 = [
[ 202105, 3 ],
[ 202107, 1 ],
[ 202108, 6 ],
];
$graph3 = combine_graphs( $graph1, $graph2 );
该函数允许组合任意数量的图形,例如...
$graph1 = [
[ 202101, 2 ],
[ 202102, 3 ],
[ 202103, 5 ],
[ 202104, 2 ],
[ 202105, 3 ],
[ 202106, 4 ],
[ 202107, 14 ],
];
$graph2 = [
[ 202105, 3 ],
[ 202107, 1 ],
[ 202108, 6 ],
];
$graph3 = [
[ 202105, 3 ],
[ 202107, 4 ],
[ 202108, 9 ],
[ 202107, 12 ],
[ 202104, 2 ],
];
$graph4 = combine_graphs( $graph1, $graph2, $graph3 );
以上会输出...
Array
(
[0] => Array
(
[0] => 202101
[1] => 2
[2] => 0
[3] => 0
)
[1] => Array
(
[0] => 202102
[1] => 3
[2] => 0
[3] => 0
)
[2] => Array
(
[0] => 202103
[1] => 5
[2] => 0
[3] => 0
)
[3] => Array
(
[0] => 202104
[1] => 2
[2] => 0
[3] => 2
)
[4] => Array
(
[0] => 202105
[1] => 3
[2] => 3
[3] => 3
)
[5] => Array
(
[0] => 202106
[1] => 4
[2] => 0
[3] => 0
)
[6] => Array
(
[0] => 202107
[1] => 14
[2] => 1
[3] => 4
)
[7] => Array
(
[0] => 202108
[1] => 0
[2] => 6
[3] => 9
)
)