Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. <?php
  2.  
  3. // i made some pretend subscriptions, but used the same variable name as you.
  4. $allMfSubs = array(
  5. 1 => array("id" => 1, "name" => "foo", "slug" => "1-foo", "for_sale" => true, "price" => 100),
  6. 2 => array("id" => 2, "name" => "bar", "slug" => "2-bar", "for_sale" => true, "price" => 200),
  7. 3 => array("id" => 3, "name" => "baz", "slug" => "3-baz", "for_sale" => true, "price" => 300),
  8. 4 => array("id" => 4, "name" => "cux", "slug" => "4-cux", "for_sale" => true, "price" => 400),
  9. 5 => array("id" => 5, "name" => "qux", "slug" => "5-qux", "for_sale" => true, "price" => 500),
  10. );
  11.  
  12. // i used the same userIdArray name you picked, but i think subscriptionIds
  13. // might be a more accurate name.
  14. //
  15. // it contains subscription id 3 and 5. you can change this to any number
  16. // combination
  17. $userIdArray = array(3, 5);
  18.  
  19. // i pass in all the subscriptions and the ids that i want
  20. function user_active_subscriptions($allMfSubs, $userIdArray) {
  21. // i create an empty array. if the user has no active subscriptions, the
  22. // function will return an empty array/no subscriptions
  23. $activeSubscriptions = array();
  24.  
  25. // i loop through the subscription ids
  26. foreach($userIdArray as $subscriptionId) {
  27. // if the subscriptionId isn't found in $allMfSubs, the
  28. // $subscription variable will have the value of null
  29. $subscription = $allMfSubs[$subscriptionId];
  30.  
  31. // an if statement will treat null and false the same way.
  32. // if the $subscription is true/not null, add it to the array of
  33. // active subscriptions
  34. if($subscription) {
  35. $activeSubscriptions[] = $subscription;
  36. }
  37. }
  38.  
  39. // return whatever you've found
  40. return $activeSubscriptions;
  41. }
  42.  
  43. // because the $allMfSubs indexes match to the nested array's id, I only care if
  44. // the $allMfSubs index exists. i dont have to care about any of the values in
  45. // the nested arrays
  46.  
  47. $results = user_active_subscriptions($allMfSubs, $userIdArray);
  48. print_r($results);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement