note 99885 added to ref.array

From: Date: Mon, 13 Sep 2010 03:17:47 +0000
Subject: note 99885 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-172504@lists.php.net to get a copy of this message
I made a simple little function to make an array (containing readable data) presentable in human form. I added some error checking, too.. (if it's not human-readable, don't include it). <?php function Combine($Input) { if(!is_array($Input)) // I only want arrays. return(false); // remove named index (only numeric index) $Input=array_values($Input); // read the array, if it's not a string or number, remove it! for($i=0;$i<count($Input);$i++) if(!is_string($Input[$i])&&!is_numeric($Input[$i])) unset($Input[$i]); // if an element got removed in the middle of the array, it leaves holes in the // array index, so we use array_values to reset them again. $Input=array_values($Input); // several array counts used asead. $Count=count($Input); if($Count>1) return(implode(', ',array_splice($Input,0,$Count-1)).' and '.$Input[0]); if($Count==1) return($Input[0]); if($Count==0) return(''); // nothing should happen beyond this point. } Combine(array('A')); // A Combine(array('A','B')); // A and B Combine(array('A','B','C')); // A, B and C Combine(array('A','B','9',4,5)); // A, B, 9, 4 and 5 Combine(array(3,false,5,'nine','?!?!')); // 3, 5, nine and ?!?! // and so on... ?> Hope this helps someone, Nitrogen. ---- Server IP: 92.48.74.199 Probable Submitter: 86.142.216.151 ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- https://master.php.net/note/edit/99885 Del: integrated -- https://master.php.net/note/delete/99885/integrated Del: useless -- https://master.php.net/note/delete/99885/useless Del: bad code -- https://master.php.net/note/delete/99885/bad+code Del: spam -- https://master.php.net/note/delete/99885/spam Del: non-english -- https://master.php.net/note/delete/99885/non-english Del: in docs -- https://master.php.net/note/delete/99885/in+docs Del: other reasons-- https://master.php.net/note/delete/99885 Reject -- https://master.php.net/note/reject/99885 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172504) next »