phparraysmultidimensional-arraymerging-data

Merge two sets of deep rows in a multidimensional array identified by related column values


I have a scenario where I am fetching data from 2 separate files - a .xml file and a .txt file - and I am trying to use PHP to combine 2 arrays (one from each file) on a matching value from each.

I don't have control over the format of the aforementioned files, so using the following code I have put together so far:

<?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 );

I am able to get the following arrays:

Array 1: .xml

[records] => Array 
(
    [PositionRecord] => Array 
    (
        [record] => Array 
        (
            [0] => Array 
            (
                [keyword] => "something", // Value to match
                [position] => "1"
           ),
           ...
        )
    )
)

Array 2: .txt

[records] => Array 
(
    [PositionRecord] => Array 
    (
        [record] => Array 
        (
            [0] => Array 
            (
                [0] => "something", // Value to match
                [1] => "1000"
           ),
           ...
        )
    )
)

How would I join these arrays on the matching keyword value to end up with an array like this:

[records] => Array 
(
    [PositionRecord] => Array 
    (
        [record] => Array 
        (
            [0] => Array 
            (
                [keyword] => "something",
                [position] => "1",
                [volume] => "1000" 
           ),
           ...
        )
    )
)

I have tried using array_merge, array_merge_recursive and array_combine however they seem only to append one array to the other. I have also tried this answer and this answer which both return an empty array [].


Solution

  • Manually merge those arrays:

    <?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>";
    

    Output:

    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"
            }
          }
        }
      }
    }