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));
4
  • error on line 11... And the error is? O.o Commented Nov 14, 2014 at 8:08
  • Take a look at this: stackoverflow.com/questions/5653241/… Commented Nov 14, 2014 at 8: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. Commented Nov 14, 2014 at 8:19
  • This question is Unclear -- it does not contain a minimal reproducible example. Commented Mar 17, 2023 at 22:02

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
Right the 3rd parameter is a function to compare the arrays
You didnt state in your question how they are compared
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.