[英]Search in a 2D array… php
I have this array: 我有这个数组:
$Fruit = array() $水果= array()
$Fruit[$species][$property] = $value $ 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 此致,Thijs
array_key_exists($property, $Fruit[$species])
->
is for objects, []
is for writing to and reading from arrays. ->
用于对象, []
用于写入和读取数组。
BTW, unless your values can be null
, I'd recommend isset
instead of array_key_exists
: 顺便说一句,除非您的值可以为null
,否则我建议使用isset
而不是array_key_exists
:
isset($Fruit[$species][$property])
Should be more intuitive. 应该更直观。
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 more 在PHP手册中查看以下循环: http : //php.net/manual/zh/control-structures.foreach.php ,并将其与if
子句结合使用以获取更多信息
I'm not giving you a direct answer cause foreach
is a part of PHP basics you need to learn. 我没有给您直接的答案,因为foreach
是您需要学习的PHP基础的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.