1

I get and error array to string conversion error on line 11 I need to compare $result array with $file array and then over write FILE with $result data. In other words, FILE and the data it contains is continuously being updated with $result

compare -> overwrite -> repeat at next execution.

Note: .db file is empty at first cycle but becomes populated at first write.

sample code with Array to string conversion error:

<?php
$id = $argv[1];  //variable for inbound
$result = array(
    'return' => array(
        array(1,2,3),
        array(6,2,3),
        array(3,2,3),
    )
);
function getdiff($new, $old) {
   $diff = array_intersect($new, $old);
   return $diff;
}
$old = file_exists('1.db') ? json_decode(file_get_contents('1.db'), 1) : array();
$arrayDiffresult = getdiff( $result, $old);
file_put_contents('1.db', json_encode($result));
print_r(
    getdiff($result, $old)
);
?>

I have a second method I have tried and I get the same error, at the comparison point line 9.

$result = array(
    'return' => array(
        array(1,2,3),
        array(5,2,3),
        array(3,2,3),
    )
);
$lines = file("myDB.db");
$arrayDiffresult = array_diff ( $result['return'], $lines);
file_put_contents('myDB.db', print_r($result['return'], true));
  • error on line 11... And the error is? O.o – Marco Mura Nov 14 '14 at 08:08
  • Take a look at this: http://stackoverflow.com/questions/5653241/using-array-intersect-on-a-multi-dimensional-array – k.tarkin Nov 14 '14 at 08:13
  • When asking questions of the form "why doesn't ______ work?", I'd suggest cutting out anything that complicates the example. In this case that's the filesystem operations. They make it difficult for others to simulate your problem, and (here) are likely making it harder for you to debug what is effectively a misuse of `array_intersect()` as well. – Don McCurdy Nov 14 '14 at 08:19
  • This question is Unclear -- it does not contain a [mcve]. – mickmackusa Mar 17 '23 at 22:02

1 Answers1

0

I believe array_intersect is only used in one dimensional arrays, and it is attempting to treat the nested arrays as a string for equality comparison. However looking at the documentation show the function array_uintersect where you can write your own comparison function as a callback. You didn't provide much information as to what the requirements are but if you do I'd be happy to help

Mauricio Trajano
  • 2,677
  • 2
  • 20
  • 25
  • Mauricio Trajano I stated the requirements very clearly compare -> overwrite -> repeat at next execution. Warning: array_uintersect(): at least 3 parameters are required, 2 given in –  Nov 14 '14 at 16:11
  • Right the 3rd parameter is a function to compare the arrays – Mauricio Trajano Nov 14 '14 at 17:51
  • You didnt state in your question how they are compared – Mauricio Trajano Nov 14 '14 at 17:52
  • exactly like the post and exactly like array_diff. array_diff the above logic would solve the issue if I could figure out the datatype issue. But I can't, so I am looking for a work around or new method to do the exact same thing as in the post. zero difference in over all logic. –  Nov 14 '14 at 18:25