Skip to content

Instantly share code, notes, and snippets.

@mamchenkov
Last active August 25, 2016 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamchenkov/f3df2bbbfadc36f004e8cc4ca373b78b to your computer and use it in GitHub Desktop.
Save mamchenkov/f3df2bbbfadc36f004e8cc4ca373b78b to your computer and use it in GitHub Desktop.
<?php
/**
* This script shows an example of how merge two arrays,
* where one array contains instances of Products, and the
* second array contains **multiple** instances of Product
* Attributes, and where the records in both arrays are
* linked via some value ($product->id == $attribute->product_id).
*/
/**
* Example Product class
*/
class Product {
protected $id;
protected $name;
public function __construct($id, $name) {
$this->setId($id);
$this->setName($name);
}
public function setId($id) {
$this->id = $id;
}
public function getId() {
return $this->id;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
/**
* Example Attribute class
*/
class Attribute {
protected $product_id;
protected $name;
protected $value;
public function __construct($product_id, $name, $value) {
$this->setProductId($product_id);
$this->setName($name);
$this->setValue($value);
}
public function setProductId($id) {
$this->product_id = $id;
}
public function getProductId() {
return $this->product_id;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
public function setValue($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$products = [
new Product(1, 'Product 1'),
new Product(2, 'Product 2'),
new Product(3, 'Product 3'),
];
$attributes = [
new Attribute(1, 'description', 'Nice product'),
new Attribute(1, 'price', 10),
new Attribute(2, 'description', 'Very nice product'),
new Attribute(2, 'price', 15),
new Attribute(3, 'description', 'Not so good product'),
new Attribute(2, 'price', 20),
];
#print_r($products);
#print_r($attributes);
$result = [];
foreach ($products as $product) {
$fullProduct = new StdClass();
$fullProduct->id = $product->getId();
$fullProduct->name = $product->getName();
$productAttributes = array_filter($attributes, function ($item) use ($product) {
// Return true for item to pass the filter.
// Return false for item to be filtered out.
return $item->getProductId() == $product->getId();
});
foreach ($productAttributes as $attribute) {
$fullProduct->{ $attribute->getName() } = $attribute->getValue();
}
$result[] = $fullProduct;
}
print_r($result);
/* OUTPUT:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Product 1
[description] => Nice product
[price] => 10
)
[1] => stdClass Object
(
[id] => 2
[name] => Product 2
[description] => Very nice product
[price] => 20
)
[2] => stdClass Object
(
[id] => 3
[name] => Product 3
[description] => Not so good product
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment