Jump to content

Recommended Posts

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!

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.