note 116908 added to function.array-search

From: Date: Thu, 19 Mar 2015 15:47:35 +0000
Subject: note 116908 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-201988@lists.php.net to get a copy of this message
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 ) ---- Server IP: 72.52.91.14 Probable Submitter: 187.11.236.135 ---- Manual Page -- http://php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/116908 Del: integrated -- https://master.php.net/note/delete/116908/integrated Del: useless -- https://master.php.net/note/delete/116908/useless Del: bad code -- https://master.php.net/note/delete/116908/bad+code Del: spam -- https://master.php.net/note/delete/116908/spam Del: non-english -- https://master.php.net/note/delete/116908/non-english Del: in docs -- https://master.php.net/note/delete/116908/in+docs Del: other reasons-- https://master.php.net/note/delete/116908 Reject -- https://master.php.net/note/reject/116908 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201988) next »