Jump to content

Recommended Posts

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 by quasiman
Link to comment
https://forums.phpfreaks.com/topic/270483-building-array-from-possible-variables/
Share on other sites

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.