Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dfns's avatar
Level 1

Extracting parts of nested array collection with condition

Hi,

i have a Twitter Api Response Json Array like this

{
  "data": {
    "author_id": "1318152273441312768",
    "id": "1544277414297190401",
    "text": "RT @bellalilli161: Horses love cuddles too❤️\n\n#love https://t.co/tSwp5mUtWP"
  },
  "includes": {
    "users": [
      {
        "id": "1318152273441312768",
        "name": "𝔾𝕠𝕝𝕕𝕚𝕖 @ Little 🐦",
        "profile_image_url": "https://pbs.twimg.com/profile_images/1343776361958273029/MV5d7StR_normal.jpg",
        "username": "LittleB32692742"
      },
      {
        "id": "1393176420592017410",
        "name": "ELEONORA",
        "profile_image_url": "https://pbs.twimg.com/profile_images/1543285566963433472/cUctf-_8_normal.jpg",
        "username": "bellalilli161"
      }
    ]
  },
  "matching_rules": [
    {
      "id": "1544277371326439426",
      "tag": "#love"
    }
  ]
}

Now i want to extract all of the data array and the incluces.users part where the user.id matches the author_id.

$tweet =  [
	"author_id": "1318152273441312768",
    "id": "1544277414297190401",
    "text": "RT @bellalilli161: Horses love cuddles too❤️\n\n#love https://t.co/tSwp5mUtWP"
    "name": "𝔾𝕠𝕝𝕕𝕚𝕖 @ Little 🐦",
    "profile_image_url": "https://pbs.twimg.com/profile_images/1343776361958273029/MV5d7StR_normal.jpg",
    "username": "LittleB32692742"
]

I played around for too many hours now and couln't get it working, pls help ! Cakephp has a powerfull extraxtion path syntax https://book.cakephp.org/4/en/core-libraries/hash.html#hash-path-syntax i used a few years ago, is there something elegant in laravel too?

I put up the sample on https://collection-playground.atymic.dev/?p=ee93ef6a8b20654bf7480b2d879a0bde , thanks!

0 likes
2 replies

Please or to participate in this conversation.