找到需要的第一个元素
我有这个数组
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
)
)
是否可以轻松地按我需要的正确顺序进行引用。所以我需要的订单是标准的、专业的、高级的..所以如果其中一个不存在,我可以像这样对另一个进行操作吗?
if (!isset($my_array['standard'])) {
$use_me = $my_array['standard']
}elseif(!isset($my_array['professional'])) {
$use_me = $my_array['professional']
}elseif(!isset($my_array['professional'])) {
$use_me = $my_array['premium']}
}
我有上面的代码,我认为可以工作,但有更好的方法吗
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['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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
应该可以做到:
简而言之:
array_keys
)name
,因此它返回standard
或第二个键是什么)。 (array_slice
)$my_array
并将其存储在$use_me
中。That should do it:
In short:
array_keys
)name
, so it returnsstandard
or whatever the second key is). (array_slice
)$my_array
using that key and store it in$use_me
.我会这样做:
或者更结构化一点:
I would do this:
or a little more structured:
是的,您可以按照代码中所示的方式进行操作。但是,您的 if 语句逻辑不正确。你的代码应该是这样的:
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:
尝试使用 array_key_exists
}
或 u可以通过 array-intersect-key
try with array_key_exists
}
or u can go through array-intersect-key
如果我正确理解您想要执行的操作,并且如果不能保证键的顺序与示例中的顺序完全相同,您可以执行以下操作:
这将遍历所有包并设置
$ use_me
变量到第一个找到的值。如果未找到值,则将其设置为默认值。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:
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.