note 103881 added to function.list

From: Date: Sun, 08 May 2011 22:37:36 +0000
Subject: note 103881 added to function.list
Groups: php.notes 
Request: Send a blank email to php-notes+get-179550@lists.php.net to get a copy of this message
Note that list(...) isn't limited to scalar variables. It can even create an associative array: <?php $html=<<<EOT <table> <tr class="row-odd"> <td><span class="username">Foo</span></td> <td><span class="userid">30185</span></td> </tr><tr class="row-even"> <td><span class="username">Bar</span></td> <td><span class="userid">3093</span></td> </tr> </table> EOT; interface RegExps { const PROFILE_CONTENT='/<tr[^>]*>.*?<span class="username">(.*?)<\/span>[ ]?.*?<span class="userid">(.*?)<\/span>.*?<\/tr>/msi'; } preg_match_all(RegExps::PROFILE_CONTENT,$html, $matches,PREG_SET_ORDER); $profiles=array(); foreach($matches as $match) { $profile=array(); list(,$profile['username'],$profile['userid'])=$match; //$profile=array_reverse($profile); $profiles[]=$profile; } echo '<pre>'.print_r($profiles,true).'</pre>'; ?> Just be careful, as the manual warns us, regarding the right-most parameter being handled first. If this becomes a problem, I'd suggest using array_reverse as shown in my code. ---- Server IP: 208.69.120.55 Probable Submitter: 76.93.19.40 ---- Manual Page -- http://www.php.net/manual/en/function.list.php Edit -- https://master.php.net/note/edit/103881 Del: integrated -- https://master.php.net/note/delete/103881/integrated Del: useless -- https://master.php.net/note/delete/103881/useless Del: bad code -- https://master.php.net/note/delete/103881/bad+code Del: spam -- https://master.php.net/note/delete/103881/spam Del: non-english -- https://master.php.net/note/delete/103881/non-english Del: in docs -- https://master.php.net/note/delete/103881/in+docs Del: other reasons-- https://master.php.net/note/delete/103881 Reject -- https://master.php.net/note/reject/103881 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179550) next »