note 116908 deleted from function.array-search by peehaa

From: Date: Fri, 07 Apr 2017 10:35:42 +0000
Subject: note 116908 deleted from function.array-search by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-209003@lists.php.net to get a copy of this message
Note Submitter: emanuel dot tavares at connix dot com dot br ---- Hi! I've tried to use array_search to fill an array with registers from a database seach with this: $registros = 0; while ( $row = $this->result->fetch_array() ) { if ( !array_search($row[cd_subsecao], $this->listaSecoes) ) $registros = array_push($this->listaSecoes, $row[cd_subsecao]); } But the result was: Array ( [0] => obr [1] => obr [2] => obr [3] => a10 [4] => obr [5] => obr [6] => obr ) It was caused by because the first element has repetitions. Then I've tried to substitute the !array_search.... for array_search(...) == FALSE, but it didn't worked too. The solution was to use the "===": $registros = 0; while ( $row = $this->result->fetch_array() ) { if ( array_search($row[cd_subsecao], $this->listaSecoes) === FALSE) $registros = array_push($this->listaSecoes, $row[cd_subsecao]); } It returned: Array ( [0] => obr [1] => a10 )

« previous php.notes (#209003) next »