1

I'm trying to filter an array that looks like this

$array = array(
    "id" => "SomeID",
    "name" => "SomeName",
    "Members" => array(
        "otherID" => "theValueIamLookingFor",
        "someOtherKey" => "something"
    )
);

Now, I'm filtering for data sets, where "otherID" is a certain value. I know I could use array_filter to filter for "id", but I can't for the life of me figure out how to filter for a value in an array inside an array.

Adding some of the data as provided by the WebAPI (I run that through json_decode to create an associative array before all this filtering business)

[
{
"id": "b679d716-7cfa-42c4-9394-3abcdged",
"name": "someName",
"actualCloseDate": "9999-12-31T00:00:00+01:00",
"members": [
  {
    "otherID": "31f27f9e-abcd-1234-aslkdhkj2j4",
    "name": "someCompany"
  }
],
"competitor": null,
},
{
"id": "c315471f-45678-4as45-457-asli74hjkl",
"name": "someName",
"actualCloseDate": "9999-12-31T00:00:00+01:00",
"members": [
  {
    "otherID": "askgfas-agskf-as",
    "name": "someName"
  }
],
"competitor": null,

}, ]

1 Answer 1

1

You can do something like:

$arr = array(
    array(
        "id" => "SomeID",
        "name" => "SomeName",
        "Members" => array (
                "otherID" => "ThisIsNottheValueIamLookingFor",
                "someOtherKey" => "something"
            )
    ),
    array(
        "id" => "SomeID",
        "name" => "SomeName",
        "Members" => array (
                "otherID" => "theValueIamLookingFor",
                "someOtherKey" => "something"
            )
    ),
    array(
        "id" => "SomeID",
        "name" => "SomeName",
        "Members" => array (
                "otherID" => "ThisIsNottheValueIamLookingForEither",
                "someOtherKey" => "something"
            )
    ),
);

$result = array_filter($arr, function( $v ){
    return $v["Members"]["otherID"] == "theValueIamLookingFor";
});

This will result to:

Array
(
    [1] => Array
        (
            [id] => SomeID
            [name] => SomeName
            [Members] => Array
                (
                    [otherID] => theValueIamLookingFor
                    [someOtherKey] => something
                )

        )

)

Here is the doc for more info: http://php.net/manual/en/function.array-filter.php


UPDATE

On you Updated array, the structure of array is different. You have to use $v["members"][0]["otherID"] to get the otherID

Please try the code below:

$result = array_filter($arr, function( $v ){
    return $v["members"][0]["otherID"] == "theValueIamLookingFor";
});
Sign up to request clarification or add additional context in comments.

7 Comments

Looks great and simple. Unfortunately, I get "PHP exception: Undefined index otherID". I swear, I'm using the correct key here. I 'll try and edit my initial question with the actual data (json, turned into array with json_decode) as it's being provided by the API.
@thom_stackoverflow Updated my answer. Please check
First: Can you explain, why the data structure would be different? I still get the an "Undefined index", though, so I'm really at a loss.
I made a fiddle: eval.in/950886... The first change is on the first array you gave.. The array key is Members with the capital M while on the second array you gave it is members (small m)
The second structural change is that.. The Members on the first array is just an associative array... While the member on the updated JSON is an array with a first element is an associative array. As you noticed there is a [0] on the second format because we have to access the first element of the array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.