我有一个场景,我从 2 个单独的文件(一个
.xml
文件和一个 .txt
文件)获取数据,并且我尝试使用 PHP 将 2 个数组(每个文件一个)组合到每个数组的匹配值上.
我无法控制上述文件的格式,因此使用到目前为止我整理的以下代码:
<?php
function dir_to_array( $dir, $se ) {
$result = array();
$cdir = scandir( $dir );
foreach ( $cdir as $key => $value ) {
$file_info = pathinfo( $value );
if ( ! in_array( $value, array( ".", ".." ) ) ) {
if ( is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) {
$result[$value] = dir_to_array( $dir . DIRECTORY_SEPARATOR . $value );
} else {
if ( $file_info['extension'] == 'xml' ) {
if ( isset( $se ) && $se !== 'undefined' ) {
if ( strpos( $value, $se ) !== false) {
$result['xml'] = xmlToArray( file_get_contents( $dir.'/'.$value ) );
}
}
}
if ( $file_info['extension'] == 'txt' ) {
$file = fopen( $dir.'/'.$value, 'r' );
$count = 0;
while ( ( $line = fgetcsv( $file ) ) !== FALSE ) {
// trying to match the structure of the above array ($result['xml'])
$result['txt']['records']['PositionRecords']['record'][$count++] = $line;
}
fclose( $file );
}
}
}
}
return json_encode( $result );
}
echo dir_to_array( 'path/to/something', $arg );
我能够得到以下数组:
数组 1: .xml
[records] => Array
(
[PositionRecord] => Array
(
[record] => Array
(
[0] => Array
(
[keyword] => "something", // Value to match
[position] => "1"
),
...
)
)
)
数组2:.txt
[records] => Array
(
[PositionRecord] => Array
(
[record] => Array
(
[0] => Array
(
[0] => "something", // Value to match
[1] => "1000"
),
...
)
)
)
我如何将这些数组连接到匹配的
keyword
值上,最终得到这样的数组:
[records] => Array
(
[PositionRecord] => Array
(
[record] => Array
(
[0] => Array
(
[keyword] => "something",
[position] => "1",
[volume] => "1000"
),
...
)
)
)
我尝试过使用
array_merge
、array_merge_recursive
和 array_combine
,但它们似乎只是将一个数组附加到另一个数组。 我也尝试过 这个答案 和 这个答案 都返回一个空数组[]
。
手动合并这些数组:
<?php
$a['records']['PositionRecord']['record'][0] = ['keyword'=>"something","position"=>"1"];
$a['records']['PositionRecord']['record'][1] = ['keyword'=>"something2","position"=>"2"];
$b['records']['PositionRecord']['record'][0] = ["something","1000"];
$b['records']['PositionRecord']['record'][1] = ["something2","2000"];
for($i = 0, $c = count($a['records']['PositionRecord']['record']); $i < $c; $i ++)
{
$a['records']['PositionRecord']['record'][$i]['volume'] = $b['records']['PositionRecord']['record'][$i][1];
}
echo "<pre>";
var_dump($a);
echo "</pre>";
输出:
array(1) {
["records"]=>
array(1) {
["PositionRecord"]=>
array(1) {
["record"]=>
array(2) {
[0]=>
array(3) {
["keyword"]=>
string(9) "something"
["position"]=>
string(1) "1"
["volume"]=>
string(4) "1000"
}
[1]=>
array(3) {
["keyword"]=>
string(10) "something2"
["position"]=>
string(1) "2"
["volume"]=>
string(4) "2000"
}
}
}
}
}
要合并嵌套在结果数组内的两个有效负载的行,请使用一个循环将关键字引用的 xml 行推送到新的结果数组中,然后使用另一个循环将 txt 行数据附加到先前引用的行(如果找到) 。 以下脚本旨在适应 xml 行可能没有相关 txt 行的可能性,反之亦然。 演示
$xmlRecords = $result['xml']['records']['PositionRecord']['record'];
$txtRecords = $result['txt']['records']['PositionRecord']['record'];
$merged = [];
foreach ($xmlRecords as $xml) {
$ref[$xml['keyword']] = $xml;
$merged[] =& $ref[$xml['keyword']];
}
foreach ($txtRecords as [$keyword, $volume]) {
if (!isset($ref[$keyword])) {
// don't bother making a new reference, just push the row
$merged[] = [
'keyword' => $keyword,
'volume' => $volume,
];
} else {
$ref[$keyword]['volume'] = $volume;
}
}
var_export($merged);
新样本输入:
$result = [
'xml' => [
'records' => [
'PositionRecord' => [
'record' => [
['keyword' => 'foo', 'position' => '1'],
['keyword' => 'example', 'position' => '2'],
['keyword' => 'bar', 'position' => '3'],
],
],
],
],
'txt' => [
'records' => [
'PositionRecord' => [
'record' => [
['foo', '1000'],
['bar', '3000'],
['test', '2000'],
]
]
]
]
];
新样本输入的输出:
array (
0 =>
array (
'keyword' => 'foo',
'position' => '1',
'volume' => '1000',
),
1 =>
array (
'keyword' => 'example',
'position' => '2',
),
2 =>
array (
'keyword' => 'bar',
'position' => '3',
'volume' => '3000',
),
3 =>
array (
'keyword' => 'test',
'volume' => '2000',
),
)