Skip to content

Instantly share code, notes, and snippets.

@Kaapiii
Created November 1, 2013 15:56
Show Gist options
  • Select an option

  • Save Kaapiii/7267504 to your computer and use it in GitHub Desktop.

Select an option

Save Kaapiii/7267504 to your computer and use it in GitHub Desktop.
array_intersection_key with on array with nested array
<?php
// Starting Point
/*array(2){
// intersection array 1
array(3) {
[2]=>
array(5) {
["id"]=>
string(1) "2"
}
[13]=>
array(5) {
["id"]=>
string(2) "13"
}
[16]=>
array(5) {
["id"]=>
string(2) "16"
}
}
// intersection array 1
array(1) {
[2]=>
array(5) {
["id"]=>
string(1) "2"
}
}
}*/
// Intersection fast from one Array
$filter = array();
$i = 0;
// loop trough manufactures and intersect each time the arrays
foreach($manufacturers as $key => $manufacturer){
$paymentMethods[] = \Payment::getPaymentMethodsByManufacturerId($key);
if(!empty($i)){
$filter = array_intersect_key($paymentMethods[$i-1], $paymentMethods[$i]);
}
$i++;
}
$this->intersectedPaymentMethods = $filter;
// Intersection Herleitung (Jannic Tschanz)
$paymentMethods = array();
foreach ($manufacturers as $id => $manufacturer) {
foreach (Payment::getPaymentMethodsByManufacturerId($id) as $id => $paymentMethod) {
if (isset($paymentMethods[$id])) {
$paymentMethods[$id] = $paymentMethods[$id]+1;
} else {
$paymentMethods[$id] = 1;
}
}
}
$intersect = array();
$manufacturersCount = count($manufacturers);
foreach ($paymentMethods as $id => $paymentMethodCount) {
if ($manufacturersCount == $paymentMethodCount) {
$intersect[] = $id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment