note 99885 deleted from ref.array by levim
| From: | levim@php.net | Date: | Tue, 06 May 2014 21:58:32 +0000 |
| Subject: | note 99885 deleted from ref.array by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199434@lists.php.net to get a copy of this message | ||
Note Submitter: Nitrogen
----
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.