在二维数组中搜索... php
我有这个数组:
$Fruit = array()
$水果[$物种][$属性] = $价值
Array
(
[Apple] => Array
(
[Green] => 4
[Spots] => 3
[Red] => 3
[Spots] => 2
)
现在我想搜索第二个数组中是否存在某个键...
我尝试了这个:
if (!array_key_exists($property, $Fruit->$species))
但它不起作用...
有人知道如何在数组的数组...?
问候, 泰斯
I have this array:
$Fruit = array()
$Fruit[$species][$property] = $value
Array
(
[Apple] => Array
(
[Green] => 4
[Spots] => 3
[Red] => 3
[Spots] => 2
)
Now I want to search if a key exists in the second array...
I tried this:
if (!array_key_exists($property, $Fruit->$species))
But it doesn't work...
Does anybody knows how to search inside an array of an array...?
Regards,
Thijs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
->
用于对象,[]
用于写入和读取数组。顺便说一句,除非您的值可以为
null
,否则我建议使用isset
而不是array_key_exists
:应该更直观。
->
is for objects,[]
is for writing to and reading from arrays.BTW, unless your values can be
null
, I'd recommendisset
instead ofarray_key_exists
:Should be more intuitive.
您可以参考这里: https://www. php.net/manual/en/function.array-key-exists.php#92355
You could reference to here: https://www.php.net/manual/en/function.array-key-exists.php#92355
如果您需要的只是搜索中的是/否(真/假)答案,则上述方法有效,但它不会返回找到的元素的附加信息(例如,来自其他数组维度)。
在 PHP 手册中查看这个循环:
http://php.net/manual/en/control-structs.foreach.php
并将其与
if
子句结合起来以获得更多信息我不会给您直接答案,因为
foreach
是您需要学习的 PHP 基础知识的一部分。The above works if all you need is a yes/no (true/false) answer on your search but it doesn't return the found element additional info (from the other array dimension, for instance).
Check out this loop in the PHP manual:
http://php.net/manual/en/control-structures.foreach.php
and combine it with an
if
clause to get moreI'm not giving you a direct answer cause
foreach
is a part of PHP basics you need to learn.