0

After searching and doing myself for a while, I decide to ask this question on Stackoverflow for advice from everyone's experiences.

What I'm trying to do is figuring out the efficient solution to check whether one array has same keys as another array, even the corresponded value is different.

Input array example,

Array(

   ['type'] => 'first',
   ['triggers'] =>  Array
         (
           ['click'] => 'action1',
           ['push'] => 'action5',
           ['pull'] => Array
              (
                  ['event3'] => 'action6',
                  ['event4'] => 'action7'
               )
        )

)

And here is the original array example which should be compared with,

Array(

   ['type'] => 'first',
   ['triggers'] =>  Array
         (
           ['click'] => 'action1',
           ['push'] => 'action2',
           ['pull'] => Array
              (
                  ['event1'] => 'action3',
                  ['event2'] => 'action4',
                  ['event3'] => 'action6'
               )
        )

)

Above examples are in a similar(even the same) array structure, however, in the real service environment, all array data are dynamic.

What I need to know from the comparison is

  1. The value of ['trigger']['push'] will be changed
  2. The value of ['trigger']['pull']['event1'] will be changed
  3. The value of ['trigger']['pull']['event2'] will be changed

I know that is possible to check via foreach loop, but the real data is composed of greater dimensional array, and it may affect the performance.

Thanks !

Mark
  • 169
  • 1
  • 8
  • Use function in [this answer](http://stackoverflow.com/a/3877494/1960712) and you are done. – Rene Korss Oct 14 '15 at 12:51
  • @ReneKorss, I did not find this post before, trying now, thanks – Mark Oct 14 '15 at 12:57
  • If you are only interested in data at specific locations in a known structure, just do so: `if($array1['trigger']['push'] !== $array2['trigger']['push'])` Probably there is more to it, but currently your question is unclear – Steve Oct 14 '15 at 12:57
  • @Steve Well, I consider I pointed that the structure of the data array is not fixed in the post and I do not know all kinds of structure about it – Mark Oct 14 '15 at 12:59
  • @ReneKorss Please check this http://codepad.org/C8Zo2JCs , I cannot understand why the output string under ['trigger'] is '3-3', which should be 'new-3-1' I thought. – Mark Oct 14 '15 at 13:04
  • @Mark You are setting value to string and then add anotehr value to that, treating it as array. This messes up things. See [my version](http://sandbox.onlinephpfunctions.com/code/bb5d43d6ff95974dd6d4adf3dd898a32c7433569). – Rene Korss Oct 14 '15 at 13:12

1 Answers1

0

have you tried using array_diff ?

Ardit Meti
  • 571
  • 5
  • 22
  • It seems this function does not support the multidimensional array, check http://codepad.org/By05I7mG – Mark Oct 14 '15 at 12:54