blackhawk Posted January 5, 2010 Share Posted January 5, 2010 I came up with the following code to find specific key value pairs from a large array called $order->products... /*Used to get the key value pair in array*/ function seekKey($array, $key, $value) { $ret = array(); for ($i=0;$i<count($array);$i++) { if ($array[$i][$key]==$value) $ret[] = $array[$i]; } return $ret; } /*Set each company as a vendor_id found in $order->products */ $ford = seekKey($order->products, 'vendors_id', '1'); $bmw = seekKey($order->products, 'vendors_id', '2'); $jeep = seekKey($order->products, 'vendors_id', '3'); if (isset($ford[0]['vendors_id'])){ print $ford . $samething; } if (isset($bmw[0]['vendors_id'])){ print $bmw . $samething; } if (isset($jeep[0]['vendors_id'])){ print $jeep . $samething; } this is fine for me if its only bmw, jeep and poniac. but anyone know of a way to cut down on this code if i had 27 companies instead of just 3? I just don't want to write 27 if conditions that only changes the company name variable inside each one. thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/187276-help-in-how-to-cut-down-on-repeat-code/ Share on other sites More sharing options...
teamatomic Posted January 5, 2010 Share Posted January 5, 2010 $ford=1; $bmw=2; $jeep=3; $saab=4; $porche=5; or make an array...then $company['ford']; $result = seekKey($order->products, 'vendors_id', "$ford"); Quote Link to comment https://forums.phpfreaks.com/topic/187276-help-in-how-to-cut-down-on-repeat-code/#findComment-988978 Share on other sites More sharing options...
blackhawk Posted January 5, 2010 Author Share Posted January 5, 2010 would setting $result to an array and using a for loop through it cut down my if conditions to one if statement? like $result[i] representing each company? thanks Quote Link to comment https://forums.phpfreaks.com/topic/187276-help-in-how-to-cut-down-on-repeat-code/#findComment-988993 Share on other sites More sharing options...
teamatomic Posted January 5, 2010 Share Posted January 5, 2010 If you want the results for all your manufacturers then yes, put it all in an array and loop through it. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187276-help-in-how-to-cut-down-on-repeat-code/#findComment-989073 Share on other sites More sharing options...
Psycho Posted January 5, 2010 Share Posted January 5, 2010 This should work to create variables using the company names, just add to the $company array as many option as you need <?php /*Used to get the key value pair in array*/ function seekKey($array, $key, $value) { $ret = array(); for ($i=0;$i<count($array);$i++) { if ($array[$i][$key]==$value) $ret[] = $array[$i]; } return $ret; } /*Set each company as a vendor_id found in $order->products */ $companies = array(1 => 'ford', 2 => 'bmw', 3 => 'jeep'); foreach ($companies as $key => $company) { $$company = array(0=>array('vendors_id'=>'asd')); seekKey($order->products, 'vendors_id', $key); if (isset(${$company}[0]['vendors_id'])){ print ${$company} . $samething; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187276-help-in-how-to-cut-down-on-repeat-code/#findComment-989098 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.