I have this array
Array
(
[name] => Step1 is here
[standard] => Array
(
[product_id] => 85,99
[product_name] => Step1 is here
[product_price] => 976.0000
[product_description] => :something
[product_image] => http://someurl.com/shop_pos/image/data/13D.png
)
[professional] => Array
(
[product_id] => 61
[product_name] => Step1 is here
[product_price] => 289.0000
[product_description] => somethingothere
[product_image] => http://someurl.com/shop_pos/image/data/13B.png
)
[premium] => Array
(
[product_id] => 677
[product_name] => Step1 is here
[product_price] => 289.0000
[product_description] => somethingothere
[product_image] => http://someurl.com/shop_pos/image/data/13A.png
)
)
Is there an easy of referencing in the proper order that i need. SO the order i need is standard, professional, premium.. so if one is not present can I do to the other like this
if (!isset($my_array['standard'])) {
$use_me = $my_array['standard']
}elseif(!isset($my_array['professional'])) {
$use_me = $my_array开发者_如何学C['professional']
}elseif(!isset($my_array['professional'])) {
$use_me = $my_array['premium']}
}
i have the above code that i think may work but is there a better way
That should do it:
$keys = array_slice(array_keys($my_array), 1, 1);
$use_me = $my_array[$keys[0]];
In short:
- Get the array keys (name, standard, professional, premium). (
array_keys
) - Get the second key (bypassing
name
, so it returnsstandard
or whatever the second key is). (array_slice
) - Reference
$my_array
using that key and store it in$use_me
.
I would do this:
foreach (array('standard', 'professional', 'premium') as $name) {
if (isset($my_array[$name])) {
$use_me = $my_array[$name];
break;
}
}
or a little more structured:
function selectPlan($array) {
foreach (array('standard', 'professional', 'premium') as $name) {
if (isset($array[$name])) return $array[$name];
}
}
$use_me = selectPlan($my_array);
Yes, you can do it the way you have presented in your code. However, your if statement logic is not correct. Your code should be like this:
if (isset($my_array['standard'])) {
$use_me = $my_array['standard'];
} elseif(isset($my_array['professional'])) {
$use_me = $my_array['professional'];
} elseif(isset($my_array['premium'])) {
$use_me = $my_array['premium'];
}
try with array_key_exists
foreach($input as $k=>$v) {
if (array_key_exists('standard', $k)) {
$output [$k] = $k;
}
if (array_key_exists('professional', $k)) {
$output [$k] = $k;
}
if (array_key_exists('premium', $k)) {
$output [$k] = $k;
}
}
or u can go through array-intersect-key
If I understand correctly what you want to do, and if the order of the keys is not guaranteed to be exactly like the one in the example, you can do something like:
// initialization
$use_me = 'default value';
// go through each package
foreach (array('standard', 'professional', 'premium') as $package) {
// when we find a package that does exist
if (isset($my_array[$package])) {
// mark it as found and exit the loop
$use_me = $package;
break;
}
}
This will go through all the packages and set the $use_me
variable to the first found value. If no value is found, it sets it to a default value.
精彩评论