Jump to content

Twig - Merge arrays where there's a matching value


JeroenB
 Share

Recommended Posts

I'm having trouble matching/merging two arrays in Twig. Let's say I have an array "tests" and an array "results". Both arrays contain a value "link_id". Now I want to merge the values of the arrays where both the ID's match.

For example these two arrays:

{
    "tests": [
       {
          "link_id": "54234",
          "value1": "test bla"
       },
       {
          "link_id": "98737",
          "value1": "bye bye"
       }
    ]
}

{
    "results": [
        {
            "link_id": "54234",
            "valueX": "test bla",
            "valueY": "blabla"
        },
        {
            "link_id": "98737",
            "valueX": "bye bye",
            "valueY": "hello"
        }
    ]
}

and I want to achieve this:

{
    "newdata": [
        {
            "link_id": "54234",
            "valueX": "test bla",
            "valueY": "blabla"
            "value1": "test bla"
        },
        {
            "link_id": "98737",
            "valueX": "bye bye",
            "valueY": "hello"
            "value1": "bye bye"
        }
    ]
}

So 'tests' and 'results' are merged where there's a matching link_id. How can I do this in Twig? Please let me know!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...