Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use array_udiff() correctly?
    text
    copied!<p>I have two multidimensional arrays that both look something like this:</p> <pre><code>Array ( [0] =&gt; Array ( 'id' =&gt; 3, 'other' =&gt; 'some string', 'timestamp' =&gt; 2000-01-01 00:00:00 ), [1] =&gt; Array ( 'id' =&gt; 6, 'other' =&gt; 'another string', 'timestamp' =&gt; 1835-01-01 00:00:00 ) ) </code></pre> <p>I'm trying to find a way to figure out which elements show up in one array (<code>$b</code>), but not the other (<code>$a</code>) and if there are any elements with changed values. If <code>$a</code> is:</p> <pre><code>Array ( [0] =&gt; Array ( 'id' =&gt; 3, 'other' =&gt; 'some string', 'timestamp' =&gt; 2000-01-01 00:00:00 ) ) </code></pre> <p>and <code>$b</code> is:</p> <pre><code>Array ( [0] =&gt; Array ( 'id' =&gt; 3, 'other' =&gt; 'some string', 'timestamp' =&gt; 2000-01-01 12:12:12 ), [1] =&gt; Array ( 'id' =&gt; 4, 'other' =&gt; 'some string', 'timestamp' =&gt; 1900-01-01 01:12:23 ) ) </code></pre> <p>Then the function should return:</p> <pre><code>Array ( [0] =&gt; Array ( 'id' =&gt; 3, 'other' =&gt; 'some string', 'timestamp' =&gt; 2000-01-01 12:12:12 ), [1] =&gt; Array ( 'id' =&gt; 4, 'other' =&gt; 'some string', 'timestamp' =&gt; 1900-01-01 01:12:23 ) ) </code></pre> <p>because the element with <code>id = 3</code> has been changed (the <code>timestamp</code> field) and the element with <code>id = 4</code> is new and doesn't appear in the other array.</p> <p>I've been trying to do this with <code>array_udiff</code>, but I still don't know how it works (it seems to sort both arrays first, but then how does it do comparison?). Is <code>array_udiff</code> the proper method or should I write a custom function?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload