How to efficiently query nested JSON relationships (reacts_with, affected_by) without redundancy?
09:02 24 Apr 2026

I am working with a structured JSON dataset where each ingredient contains relational metadata such as:

  • reacts_with

  • affected_by

  • compatible_with

A simplified structure is like this:

{
  "category": "surfactants",
  "ingredients": [
    {
      "name": "sodium stearate",
      "reacts_with": ["calcium", "magnesium"],
      "affected_by": ["hard_water"],
      "compatible_with": ["nonionic_surfactants"]
    }
  ]
}

The full dataset (for reference): https://cleanformulation.com/data/ingredients-dataset.json

I am trying to find:

  1. all ingredients affected by "hard_water"
  2. all ingredients that react with "calcium"

I am currently using a simple PHP loop like this:


This works for small data, but becomes inefficient as the dataset grows, I have to loop through all categories and ingredients every time
It feels redundant when querying multiple relationships

Is there a better way in PHP to query these relationships without full iteration each time?

php json