quasiman Posted November 9, 2012 Share Posted November 9, 2012 (edited) Hi, I'm building an array from two other arrays, and I want to loop through them for specific key/value pairs. The problem is that some of the pairs do not exist in every sub array, and so referencing them causes an unidentified offset error. So....here's what I'm doing so far. How can I (easily and with minimum if/else statements) check whether they exist prior to generating my array? <?php $category = 'linux_hosting_plan'; $name = 'Starter'; print_r(returnHostPlan($category, $name)); function returnHostPlan($category, $name) { $planDetails = getData('products/plan-details'); $customerPrice = getData('products/customer-price'); $plan = combineArray($planDetails['hosting'], $customerPrice['hosting']); foreach ($plan as $key => $value) { if ($value[0]['category'] == $category && $value[0]['plan_name'] == $name) { $array = array( 'key' => $key, 'plan_name' => $value[0]['plan_name'], 'dbspace' => $value[0]['dbspace'], 'status' => $value[0]['status'], 'webspace' => $value[0]['webspace'], 'no_of_mail_accounts' => $value[0]['no_of_mail_accounts'], 'bandwidth' => $value[0]['bandwidth'], 'hosting_location' => $value[0]['hosting_location'], 'noofdbs' => $value[0]['noofdbs'], '12' => $value[1]['add'][12], '36' => $value[1]['add'][36], '48' => $value[1]['add'][48] ); } } return $array; } ?> This creates: Notice: Undefined offset: 48 in /../../planDetails.php on line 35 Array ( [key] => 737167 [plan_name] => Ecommerce [dbspace] => 5120 [status] => Active [webspace] => 20480 [no_of_mail_accounts] => 100 [bandwidth] => 204800 [hosting_location] => us [noofdbs] => -1 [12] => 4.99 [24] => 3.99 [36] => 2.99 [48] => ) Edited November 9, 2012 by quasiman Quote Link to comment https://forums.phpfreaks.com/topic/270483-building-array-from-possible-variables/ Share on other sites More sharing options...
Pikachu2000 Posted November 9, 2012 Share Posted November 9, 2012 if( array_key_exists . . . Quote Link to comment https://forums.phpfreaks.com/topic/270483-building-array-from-possible-variables/#findComment-1391207 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.