0

I have an object array built like this (output of a "var_dump()" call i sanitized for the question a bit):

sObjects:array(3) {
  [0]=>
  object(SObject)#1 (2) {
    ["type"]=>
    string(9) "Course__c"
    ["fields"]=>
    array(1) {
      ["Id__c"]=>
      string(3) "111"
    }
  }  
  [1]=>
  object(SObject)#2 (2) {
    ["type"]=>
    string(9) "Course__c"
    ["fields"]=>
    array(1) {
      ["Id__c"]=>
      string(3) "222"
    }
  }
  [2]=>
  object(SObject)#3 (2) {
    ["type"]=>
    string(9) "Course__c"
    ["fields"]=>
    array(1) {
      ["Id__c"]=>
      string(3) "333"
    }
  }
}

Now, lets say i have $id = "111" How would i go about iterating over my object array and retrieve the array key where [id__c] has a value equal to $id? for example in this case i would expect to get back 0.

3
  • Is there anything you've tried so far? Commented Mar 5, 2017 at 14:02
  • What do you mean by "best way"? Is this the fastest way? In this case it's better to tell about how this data is generated (e.g. it is read from database, calculated somehow, etc.) Commented Mar 5, 2017 at 14:04
  • Tried accessing it in a while loop with the fetch_object() function, accessing $object->fields->id__c and $object->id__c. Tried using a foreach($array as $object) and again access it the same way and it didn't work (not to mention i have no idea how to get the array key in that method) Commented Mar 5, 2017 at 14:05

1 Answer 1

2

Use array_filter like this:

$array = [
    [
    "type" => "Course__c",
    "fields" => ["Id_c" => "111"]
    ],
    [
    "type" => "Course__c",
    "fields" => ["Id_c" => "222"]
    ]
];

$result = array_filter($array,
    function($element) {
        return $element['fields']['Id_c'] == "111" ? true :false;
    });

print_r($result); 

Will output:

Array
(
    [1] => Array
    (
        [type] => Course__c
        [fields] => Array
            (
                [Id_c] => 111
            )

    )

    )
  • For the Sobject version, replace $element['fields']['Id_c'] with $element->fields['Id_c']

  • Also if you would like to pass a variable inside the callback function use:

    $result = array_filter($array,
        function($element) use($externalVariable){
            return $element['fields']['Id_c'] == $externalVariable ? true :false;
    });
    
Sign up to request clarification or add additional context in comments.

6 Comments

Now i see that you have an array with objects, can you post the "SObject" code, you need to access the objects methods to get to it's fields, it could be something like $element->getFields()[0] instead f $element['fields']['Id_c']
What do you mean by "sObject code"? Do you mean how my sObject is declared?
The array you have posted it's made of "object(SObject)#" objects, objects have setters getters, public properties and other methods. It would help if you could post the object code, it would start with class SObject{ /// some code}. Give it a try with $element->fields['Id_c'] instead of $element['fields']['Id_c']
Yes! using $element->fields[Id__c] worked! And since i use your filter suggestion i don't even need the keys of the array since filtering it was my goal anyway. Thank you very much!
I encountered another issue now.. as i said before i am using a string array of ids to filter my object array by. But how would i pass the string into the filter method?
|

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.