1

Hello i am trying to return the differences between two arrays but i am gettig this error that i do not understand, this is my code:

$unMatched = array_diff($matchedTickets, $cart['tickets']);

These are the two arrays:

$cartTickets:

array (size=1)
  0 => 
    array (size=5)
      'id' => int 13
      'class' => string 'Regular' (length=7)
      'description' => string 'Lorem ipsum dolor' (length=17)
      'fee' => int 100
      'quantity' => int 1

$matchedTickets:

array (size=1)
  0 => 
    array (size=3)
      'id' => int 13
      'class' => string 'Regular' (length=7)
      'fee' => float 100

Can somebody please tell me what i am doing wrong here?

user3718908x100
  • 7,939
  • 15
  • 64
  • 123
  • 1
    What you're doing wrong is confusing json with PHP arrays – Mark Baker Sep 20 '15 at 16:05
  • No they are actually both arrays (at least the first one is) they are convertered to json when i return them. – user3718908x100 Sep 20 '15 at 16:06
  • 1
    Then perhaps you should actually show us the value of the arrays.... var_dump() them to show what they actually contain.... showing us the json encoding is simply confusing the issue – Mark Baker Sep 20 '15 at 16:07
  • When i use array() in place of $cart['tickets'] i don't get an error so my guess is something is wrong with that array, i know it is an array because when i use json_decode() on it i get an error saying parameter is expected to be string but array given. – user3718908x100 Sep 20 '15 at 16:08
  • 1
    So is `$cart['tickets']` an array or an object? Use var_dump() and it will tell you – Mark Baker Sep 20 '15 at 16:10
  • 1
    Yes i just did, updated my post – user3718908x100 Sep 20 '15 at 16:11
  • I see that $cart is an array but is $cart['tickets'] an array? Should you be doing array_diff($matchedTickets, $cart)? – Richard - Rogue Wave Limited Sep 20 '15 at 16:26
  • All this back and forth is a waste of time! Please present your [testcase](http://stackoverflow.com/help/mcve) so we don't have to guess/assume/take your word for it. – Lightness Races in Orbit Sep 20 '15 at 20:08
  • @Lightness Races in Orbit I assume you gave me the downvote, i already solved my problem but what do you mean? I have stated the error i got i have also posted the var_dump of the 2 arrays along with the line at which the error is thrown, i do not know what else to feed you, all information relevant to reproducing the error where given, you should take your time and go read my post. My issue has been resolved. O_O – user3718908x100 Sep 20 '15 at 20:38
  • I mean post a [minimal testcase](http://stackoverflow.com/help/mcve). SO questions and answers are for all time and for future readers as much as for yourself, and at present this Q&A isn't useful. – Lightness Races in Orbit Sep 20 '15 at 21:33
  • Already answered here : https://stackoverflow.com/questions/20695139/array-diff-gives-array-to-string-conversion-error-in-drupal – Romain Sickenberg Oct 09 '18 at 07:34

1 Answers1

1

I had to add the index 0 to each of them to get it to work. I dont know why they're like that, but it works now.

Anders
  • 8,307
  • 9
  • 56
  • 88
user3718908x100
  • 7,939
  • 15
  • 64
  • 123