2011-03-22 182 views
1

我有一個數組下面,我需要訪問checkbox3數組,並找出它是否設置。訪問多維數組值

像這樣的事情,但它不工作:

<?php if(isset($fields['checkbox3']["One"])): ?> 
    One is set 
<?php endif; ?> 

array(2) { 
    ["checkbox2"]=> 
    array(1) { 
    [0]=> 
    string(10) "Don't Show" 
    } 
    ["checkbox3"]=> 
    array(5) { 
    [0]=> 
    string(3) "One" 
    [1]=> 
    string(3) "Two" 
    [2]=> 
    string(5) "Three" 
    [3]=> 
    string(4) "Four" 
    [4]=> 
    string(5) "Five" 
    } 
} 

回答

3
in_array("One", $fields["checkbox3"]); 

使用in_array().

bool in_array (mixed $needle , array $haystack [, bool $strict ]) 
+0

感謝邁克,你說的最快,所以我會在6分鐘內接受你的答案 – 2011-03-22 23:53:03

+1

聽起來不錯。祝你好運,你在做什麼! – 2011-03-22 23:53:29

1
<?php if(isset($fields['checkbox3'][0])): ?> 
    One is set 
<?php endif; ?> 

或使用

in_array()

+0

對不起,忘記提及,陣列大小會波動 – 2011-03-22 23:53:33

2

$fields['checkbox3']["One"]不存在,而"One"是您要使用的in_array()函數,該函數的$fields['checkbox3'][0]

+0

感謝回覆climage – 2011-03-22 23:54:17

2

值:

if (in_array("One", $fields["checkbox3"])) { 

它查找字符串是否存在的數組中的條目。所以你不必知道索引或者自己遍歷數組。

+0

優秀的答案,謝謝! – 2011-03-22 23:52:06