簡體   English   中英

在php中過濾數組中的特定列

[英]Filter specific column in array in php

我正在嘗試使用下面的代碼過濾php數組中的特定列:

(strpos( 'meeting',$event['categories'] ) == false )

它實際上沒有工作。 [類別]保留的一個示例是:

$event['categories'] = 'meeting;skype'

提前致謝!

您需要將參數翻轉到strpos()

if (strpos($event['categories'],  'meeting') === false) {
    echo $event['categories'] . ' does not contain "meeting"';
}

另外,使用嚴格的比較( === vs == ),因為meeting可能在字符串的開頭,然后strpos()將返回0 ,其結果為false (在這種情況下將是錯誤的)。

供參考,請參閱:

有關示例,請參見:

我認為您應該使用===而不是==並翻轉參數

(strpos($event['categories'] , 'meeting') === false )

strpos可能返回0false並且當您使用==零就像false

查看壓縮運算符

請參閱strpos()文檔

<?php

$event = ['categories' => 'meeting;skype'];
$needle = 'meeting';
$haystack = $event['categories'];
if( ($pos = strpos( $haystack, $needle )) === false){
   echo "\n$needle not found in $haystack";
} 
else
{
     echo "\n$needle exists at position $pos in $haystack";
}

觀看演示

需要注意的兩件事是strpos()的參數順序,以及使用標識運算符(“ ===”)進行嚴格比較,以便當“針”出現在“干草堆”的零位置時'如果使用等號運算符(“ ==”),就不會錯誤地將其視為錯誤結果,因為在PHP中,零== false。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM