Remove part of associative array
本问题已经有最佳答案,请猛点这里访问。
我想搜索一个关联数组,当我找到一个值时,删除该数组部分。
以下是我的数组示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Array ( [0] => Array ( [id] => 2918 [schoolname] => Albany Medical College [AppService] => 16295C0C51D8318C2 ) [1] => Array ( [id] => 2919 [schoolname] => Albert Einstein College of Medicine [AppService] => 16295C0C51D8318C2 ) [2] => Array ( [id] => 2920 [schoolname] => Baylor College of Medicine [AppService] => 16295C0C51D8318C2 ) } |
我要做的是在
这是迄今为止我的代码:
1 2 3 4 5 | foreach($this->schs_raw as $object) { if($object['AppService'] =="16295C0C51D8318C2") { unset($object); } } |
Will Help(http://php.net/manual/en/function.array-filter.php)
1 2 3 4 5 6 | $yourFilteredArray = array_filter( $this->schs_raw, function($var) { return $object['AppService'] !="16295C0C51D8318C2" } ); |
试试这个
1 2 3 4 5 6 | foreach($this->schs_raw as $key=>$object) { if($object['AppService'] =="16295C0C51D8318C2") { unset($this->schs_raw[$key]); // unset the array using appropriate index break; // to exit loop after removing first item } } |
试试这个
1 2 3 4 5 | foreach ($this->schs_raw as &$object) { if($object['AppService'] =="16295C0C51D8318C2") { unset($object); } } |
也许吧
ZZU1
为什么不创建一个新的阵列?如果没有匹配,将索引添加到新的阵列。如果找到了,别添加你的新阵列将只包含你想要的数据。