1

I would like to compare two arrays of objects in PHP. But in most efficient way. I would like if value of object in array1 exist in any of object in array2. If so i would like to get it's ID's.

Array that I'm taking values to comprassion looks like that:

array(2) {
  [0] =>
  class stdClass#529 (4) {
    public $value =>
    string(4) "Name"
    public $valueType =>
    string(4) "text"
    public $propertyName =>
    string(13) "beeforwhiskey"
    public $propertyType =>
    NULL
  }
  [1] =>
  class stdClass#530 (4) {
    public $value =>
    string(5) "Email"
    public $valueType =>
    string(4) "text"
    public $propertyName =>
    string(5) "email"
    public $propertyType =>
    string(4) "text"
  }
}

The array that I would like to get ID's from looks like this:

array(37) {
  [0] =>
  class stdClass#532 (9) {
    public $customFieldId =>
    string(5) "xyz"
    public $href =>
    string(50) "..."
    public $name =>
    string(7) "address"
    public $fieldType =>
    string(4) "text"
    public $format =>
    string(4) "text"
    public $valueType =>
    string(6) "string"
    public $type =>
    string(4) "text"
    public $hidden =>
    string(5) "false"
    public $values =>
    array(0) {
    }
  }
  [1] =>
  class stdClass#538 (9) {
    public $customFieldId =>
    string(5) "zyx"
    public $href =>
    string(50) "..."
    public $name =>
    string(3) "age"
    public $fieldType =>
    string(13) "single_select"
    public $format =>
    string(13) "single_select"
    public $valueType =>
    string(6) "string"
    public $type =>
    string(13) "single_select"
    public $hidden =>
    string(5) "false"
    public $values =>
    array(5) {
      [0] =>
      string(5) "18-29"
      [1] =>
      string(5) "30-44"
      [2] =>
      string(5) "45-59"
      [3] =>
      string(3) "60+"
      [4] =>
      string(3) "<18"
    }
  }
.
.
.

So in this case I'm taking each object from array1, check the propertyName and if it exists in any of object in array2 in name field, then I would like to take it's id from array2.

I did this with some foreach'es but I know it's not the best way to do this. How can I make it shorter, more clear and less memory taking?

1
  • This sounds like this is a question for Codereview.SE, a site purely created to have your code reviewed by others. This would of course mean you have to include your code there, but that should be the way to go. Commented Sep 26, 2019 at 9:48

2 Answers 2

2

I'm not sure if I'm using the right columns as the values don't seem to match up, but I hope you can change them if required.

This first re-indexes the second array so that the column you want to search by is the key of an associative array, done using array_column(). From then on, you can just access this array using the field from array1 as the index.

I've added the ?? "Not found", if you need to check this, you can also use isset($array2[$element->propertyName]) and do something appropriate.

// Index array2 by the 'name' value
$array2 = array_column($array2, null, "name");

foreach ( $array1 as $element)  {
    // Use the propertyName value from array1 to find details
    echo $array2[$element->propertyName]->customFieldId ?? "Not found".PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This solution is shorter than mine, and it's quite easier to understand than the answer below. Thank you very much.
0
$arr1 = [...]; // Your input array1
$arr2 = [...]; // Your input array2

$propertyNames = array_map(function ($o) {
    return $o->propertyName;
}, $arr1);

$res = array_map(function ($o) {
    return $o->customFieldId;
},
    array_filter($arr2, function ($o) use ($propertyNames) {
        return in_array($o->name, $propertyNames, true);
    })
);

1 Comment

Some explanation of your code make the answer more understandable for readers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.