0

So, I'm new to php and while testing some services, I have a JSON object:

{
  "entry": [
    {
      "authorId": "@me",
      "type": "USER"
    },
    {
      "authorId": "514",
      "type": "USER"
    },
    {
      "authorId": "516",
      "type": "USER"
    }
  ],
  "count": 3,
  "totalResults": 3
}

and I need to assert that '@me' is part of this object. I tried something like:

$Obj->assertTrue(array_key_exists('@me', $response['entry']));

but it won't just work. Can someone give me a hint regarding this? Thank you

EDIT: '@me' can be anywhere in the array, not on the first position and I'm asking this for a a test in codecept, so I need to an assert directly

Andrei Manolache
  • 766
  • 1
  • 8
  • 17
  • 1
    'authoriId' is the array key for that value. try replacing array_key_exists with `in_array` to check the values. – Gavin Jul 22 '20 at 08:14
  • @Gavin please do not suggest solutions as comments. https://meta.stackexchange.com/a/296481/352329 – mickmackusa Jul 22 '20 at 08:17
  • @Andrei now that this page is closed, I can only offer suggests as comments. If you want something that will neatly fit inside of the `assertTrue()` call, then here are a couple options: https://3v4l.org/SNlll – mickmackusa Jul 22 '20 at 08:33
  • 1
    @Andrei since you have only received incorrect answers and while there are no upvoted answers on your question, you have the ability to self-delete your question (it will likely get deleted anyhow). – mickmackusa Jul 22 '20 at 08:40

1 Answers1

1

You can loop through your result set to check it for each entry.

function checkEntries($object, $search) {
   $object = json_decode($object, true);
   foreach($object['entry'] as $entry) {
      if ($entry['authorId'] == $search) {
          return true;
      }
   }
   return false;
}

Or you can use this:
PHP multidimensional array search by value

Thrallix
  • 699
  • 5
  • 20