1

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 the 2 unequal 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 - contains 520 elements

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

Array 2: .txt - contains 260 elements

[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 [].

Community
  • 1
  • 1
wper
  • 352
  • 2
  • 13
  • may be this question asked today by a well reputed person... – Murad Hasan May 24 '16 at 09:14
  • Why are you making multidimensional array like `['records']['PositionRecords']['record']` if it is not needed? – Rohan Kumar May 24 '16 at 09:28
  • Did you try this one? http://php.net/manual/en/function.array-diff-assoc.php see the array_diff_assoc_recursive in the comments bellow on that page – Richard May 24 '16 at 09:28
  • @RohanKumar I initally thought perhaps it might help to match the values as that is the structure of the first array. However I do know that it is not really necessary – wper May 24 '16 at 09:30

1 Answers1

1

Try to create simple array so that the traversing would be easy like

$result['txt']['records'][$count++] = ....

instead of

$result['txt']['records']['PositionRecords']['record'][$count++] = ....

Now you need to use some logic like,

$xmlArray=$result['xml']['records'][having xml data];//let
$txtArray=$result['txt']['records'][having text file data];//let
$resultArray=array();

// loop for xml data
foreach($xmlArray as $key => $value){
   $keyword= $value['keyword'];// get the keyword from xml
   // and search it in text file array
   foreach($txtArray as $k=>$v){
       if($keyword === $v[0]){ // if something matched
          // then add to the result array
          $resultArray[] = array(
                       'keyword'=>$keyword,
                       'volume'=>$v[1],
                       'position'=>$value['position']
                     );

       }
   }
}

Hopefully, this may help to solve your problem.

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • http://stackoverflow.com/questions/12051782/php-array-merge-recursive-preserving-numeric-keys – Richard May 24 '16 at 09:45
  • 1
    @Richard If both the array having same keys like `keyword` then the above link could be useful, but here keys are not same and one more thing here OP needs to match the value. – Rohan Kumar May 24 '16 at 09:48
  • @wper may be you need to change the code according to your data, I have given an idea, how you can reach to fulfill your requirements. – Rohan Kumar May 24 '16 at 10:42
  • @RohanKumar, yeah a few small changes and it is now fully working as expected, thank you so much, very much appreciated! – wper May 24 '16 at 11:41
  • Great it was helpful for you. – Rohan Kumar May 24 '16 at 11:44