speedvees
speedvees

Reputation: 117

Most efficient way to compare two arrays of objects in PHP

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?

Upvotes: 1

Views: 1592

Answers (2)

Nigel Ren
Nigel Ren

Reputation: 57131

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;
}

Upvotes: 2

liquid207
liquid207

Reputation: 226

$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);
    })
);

Upvotes: 0

Related Questions