Skip to content

Instantly share code, notes, and snippets.

@daneharrigan
Created July 23, 2016 23:07
Show Gist options
  • Save daneharrigan/9cae850e1249af62d9959abe7bbb5aa7 to your computer and use it in GitHub Desktop.
Save daneharrigan/9cae850e1249af62d9959abe7bbb5aa7 to your computer and use it in GitHub Desktop.
<?php
// i made some pretend subscriptions, but used the same variable name as you.
$allMfSubs = array(
1 => array("id" => 1, "name" => "foo", "slug" => "1-foo", "for_sale" => true, "price" => 100),
2 => array("id" => 2, "name" => "bar", "slug" => "2-bar", "for_sale" => true, "price" => 200),
3 => array("id" => 3, "name" => "baz", "slug" => "3-baz", "for_sale" => true, "price" => 300),
4 => array("id" => 4, "name" => "cux", "slug" => "4-cux", "for_sale" => true, "price" => 400),
5 => array("id" => 5, "name" => "qux", "slug" => "5-qux", "for_sale" => true, "price" => 500),
);
// i used the same userIdArray name you picked, but i think subscriptionIds
// might be a more accurate name.
//
// it contains subscription id 3 and 5. you can change this to any number
// combination
$userIdArray = array(3, 5);
// i pass in all the subscriptions and the ids that i want
function user_active_subscriptions($allMfSubs, $userIdArray) {
// i create an empty array. if the user has no active subscriptions, the
// function will return an empty array/no subscriptions
$activeSubscriptions = array();
// i loop through the subscription ids
foreach($userIdArray as $subscriptionId) {
// if the subscriptionId isn't found in $allMfSubs, the
// $subscription variable will have the value of null
$subscription = $allMfSubs[$subscriptionId];
// an if statement will treat null and false the same way.
// if the $subscription is true/not null, add it to the array of
// active subscriptions
if($subscription) {
$activeSubscriptions[] = $subscription;
}
}
// return whatever you've found
return $activeSubscriptions;
}
// because the $allMfSubs indexes match to the nested array's id, I only care if
// the $allMfSubs index exists. i dont have to care about any of the values in
// the nested arrays
$results = user_active_subscriptions($allMfSubs, $userIdArray);
print_r($results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment