quasiman Posted November 7, 2012 Share Posted November 7, 2012 Hi, Nice new forum! I'm writing a function that combines two arrays, and based on the params assigned returns a new array. The problem I'm having is making one of the parameters not required. This would return all results not filtered by the param, or if included would only return the filtered results. Here's where I am so far... <?php function showPlan($type, $cat, $name=null) { $planDetails = getData('products/plan-details'); $customerPrice = getData('products/customer-price'); $a1 = $planDetails[$type]; $a2 = $customerPrice[$type]; $a3 = combineArray($a1, $a2); //external function combining the two above arrays $array = array(); foreach ($a3 as $keys) { if ($keys[0]['category'] == $cat) { if(isset($name) && $keys[0]['plan_name'] == $name) { foreach ($keys[0] as $key => $value) { $array[$key] = $value; } foreach ($keys[1]['add'] as $key => $value) { $array[$key] = $value; } } } } return $array; } ?> Now, if I take out the line "if(isset($name) && $keys[0]['plan_name'] == $name)" and force the $name param, this function works fine. But I don't want to write two functions simply to return all results or only one result. Any help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/270381-function-paramnull-array-foreach-check-for-param/ Share on other sites More sharing options...
requinix Posted November 7, 2012 Share Posted November 7, 2012 So include the result if there's a $name and it matches, or include the result if there was no $name given? if ((name is given AND it matches) OR (name is not given)) Quote Link to comment https://forums.phpfreaks.com/topic/270381-function-paramnull-array-foreach-check-for-param/#findComment-1390654 Share on other sites More sharing options...
quasiman Posted November 7, 2012 Author Share Posted November 7, 2012 Well now I feel dumb. LOL! Thanks..... Quote Link to comment https://forums.phpfreaks.com/topic/270381-function-paramnull-array-foreach-check-for-param/#findComment-1390656 Share on other sites More sharing options...
50r Posted November 7, 2012 Share Posted November 7, 2012 i am not that good still learning though but in my own opinion i think you have mixed things here. the better way to check i think would be to do a var_dump($a3) because it seems to me that this if(isset($name) && $keys[0]['plan_name'] == $name) { evaluate to false Quote Link to comment https://forums.phpfreaks.com/topic/270381-function-paramnull-array-foreach-check-for-param/#findComment-1390659 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.