[英]Excluding array keys that start with '#' from foreach in PHP?
我有一個Drupal表單數組,它使用前綴“#”表示給定的數組鍵包含元數據而不是實際值。 我如何遍歷所有數組元素,但鍵以“#”開頭的元素除外?
foreach( $array as $key => $value ) {
if( $key[0] === "#" ) {
continue;
}
//Do work
}
嘗試這個:
<?php
function deleteElements(&$v, $k) {
global $newArray;
if(substr($k, 0, 1) !== '#') {
$newArray[$k] = $v;
}
}
$arr = array('as'=>'Test','#df'=>'this will not come','gh'=>'no test','#e'=>'again!');
$newArray = array(); // this will contain non-metadata keys
array_walk( $arr, 'deleteElements' );
//$newArrayis now..
$newArray = array('as'=>'Test','gh'=>'no test');
?>
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.