Jump to content

[SOLVED] Function that limits array results


Bifter

Recommended Posts

Hi All,

 

I have a function that limits the results returned from an array based on user input:

 

<?php
function selectFromArray($prefix="", $productArray=array()) {

    return array_filter($productArray,
        create_function('$element', 
             'return (stripos($element[1],'.var_export($prefix, true).') === 0); '));
}
?>

 

The output is returned with the below:

 

<?php
$list = selectFromArray($setype, $list);
foreach($list as $r) 
{ 
$row_color = ($row_count % 2) ? $color1 : $color2; 
$size2 = $r[2]; 
echo "<tr>
        <td id=\"id\"><span id=\"non_sorting_header\">" .$r[0]. "</span></td>
        <td id=\"name\"><span id=\"non_sorting_header\">" .$r[1]. "</span></td>
        <td id=\"speed\"><span id=\"sorting_header\">" .kMGTB2($size2). "</span></td>
	<td id=\"download\"><span id=\"sorting_header\">" .$r[3]. " Gb<br />per month</span></td>
        <td id=\"contract\"><span id=\"sorting_header\">1<br />month</span></td>
        <td id=\"info\"><span id=\"non_sorting_header\">".$r[5]."</span></td>
        <td id=\"buy\"><span id=\"non_sorting_header\">".$r[4]."<br />".$r[6]."</span></td>
      </tr>"; 
$row_count++; 
} 
?>

 

All the above works great, however I cannot work a way that if the user doesnt filter the results, i.e $setype is empty then all the entries in the array are returned.

 

Thanks,

B

<?php
function selectFromArray($prefix="", $productArray=array()) {

    if(!strlen($prefix)) return $productArray();
    return array_filter($productArray,
        create_function('$element', 
             'return (stripos($element[1],'.var_export($prefix, true).') === 0); '));
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.