Bifter Posted June 22, 2009 Share Posted June 22, 2009 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 Link to comment https://forums.phpfreaks.com/topic/163234-solved-function-that-limits-array-results/ Share on other sites More sharing options...
rhodesa Posted June 22, 2009 Share Posted June 22, 2009 <?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); ')); } ?> Link to comment https://forums.phpfreaks.com/topic/163234-solved-function-that-limits-array-results/#findComment-861248 Share on other sites More sharing options...
Bifter Posted June 22, 2009 Author Share Posted June 22, 2009 Thanks for the reply. I get Fatal error: Function name must be a string on line 437 which is: <?php if(!strlen($prefix)) return $productArray(); ?> Is there another way to do this? Link to comment https://forums.phpfreaks.com/topic/163234-solved-function-that-limits-array-results/#findComment-861260 Share on other sites More sharing options...
Jibberish Posted June 22, 2009 Share Posted June 22, 2009 just remove the () from the end of $productArray Link to comment https://forums.phpfreaks.com/topic/163234-solved-function-that-limits-array-results/#findComment-861261 Share on other sites More sharing options...
Bifter Posted June 22, 2009 Author Share Posted June 22, 2009 Thanks! Working. Link to comment https://forums.phpfreaks.com/topic/163234-solved-function-that-limits-array-results/#findComment-861273 Share on other sites More sharing options...
rhodesa Posted June 22, 2009 Share Posted June 22, 2009 just remove the () from the end of $productArray yeah...sorry...coffee hasn't fully kicked in yet Link to comment https://forums.phpfreaks.com/topic/163234-solved-function-that-limits-array-results/#findComment-861274 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.