phdphd Posted September 23, 2014 Share Posted September 23, 2014 Hi All, I want to copy into a table values from another table that partially match a given value, case-insensitively. So far I do as follows but I wonder whether there is a quicker way. $input_table=array('1'=>'toto','2'=>'tota','3'=>'hello','4'=>'TOTO','5'=>'toto'); $input_table_2 = array_map('strtolower', $input_table); $value_to_look_for='Tot'; $value_to_look_for_2=strtolower($value_to_look_for); $output_table=array(); foreach ($input_table_2 as $k=>$v) { if(false !== strpos($v, $value_to_look_for_2)) { $output_table[]=$input_table[$k]; } } One drawback is that $input_table_2 is 'foreached' whereas there might be no occurrences, which would lead to a loss of time/resources for big arrays. Thanks. Link to comment https://forums.phpfreaks.com/topic/291231-copy-into-a-table-values-from-another-table-that-partially-match-a-given-value-case-insensitively/ Share on other sites More sharing options...
Barand Posted September 23, 2014 Share Posted September 23, 2014 you could use array_filter() in conjunction with stripos() to save those strtolower() conversions $input_table=array('1'=>'toto','2'=>'tota','3'=>'hello','4'=>'TOTO','5'=>'toto'); $output_table = array_filter($input_table, function($v){return stripos($v, 'Tot')!==false;}); Link to comment https://forums.phpfreaks.com/topic/291231-copy-into-a-table-values-from-another-table-that-partially-match-a-given-value-case-insensitively/#findComment-1491855 Share on other sites More sharing options...
phdphd Posted September 23, 2014 Author Share Posted September 23, 2014 Thank you. It works great. However, it does not work when using a variable as the string to look for (here "$to_search"): $input_table=array('1'=>'toto','2'=>'tota','3'=>'hello','4'=>'TOTO','5'=>'toto'); $to_search='Tot'; $output_table = array_filter($input_table, function($v){return stripos($v, $to_search)!==false;}); var_dump($output_table); I get array(0) { } Link to comment https://forums.phpfreaks.com/topic/291231-copy-into-a-table-values-from-another-table-that-partially-match-a-given-value-case-insensitively/#findComment-1491856 Share on other sites More sharing options...
phdphd Posted September 23, 2014 Author Share Posted September 23, 2014 I got it $input_table=array('1'=>'toto','2'=>'tota','3'=>'hello','4'=>'TOTO','5'=>'toto'); $tosearch='Tot'; $output_table = array_filter($input_table, function($v) use ($tosearch){return stripos($v, $tosearch)!==false;}); var_dump($output_table); Result : array(4) { [1]=> string(4) "toto" [2]=> string(4) "tota" [4]=> string(4) "TOTO" [5]=> string(4) "toto" } Link to comment https://forums.phpfreaks.com/topic/291231-copy-into-a-table-values-from-another-table-that-partially-match-a-given-value-case-insensitively/#findComment-1491857 Share on other sites More sharing options...
Jacques1 Posted September 23, 2014 Share Posted September 23, 2014 I have no idea why you would use a closure when the same thing can be done with a simple loop: <?php $input_table=array('1'=>'toto','2'=>'tota','3'=>'hello','4'=>'TOTO','5'=>'toto'); $look_for = 'Tot'; $found = array(); foreach ($input_table as $word) { if (stripos($word, $look_for) !== false) { $found[] = $word; } } var_dump($found); Or is this not “fancy” enough? I can tell you that when it comes to programming, clarity is much more important than cool-looking code. Link to comment https://forums.phpfreaks.com/topic/291231-copy-into-a-table-values-from-another-table-that-partially-match-a-given-value-case-insensitively/#findComment-1491858 Share on other sites More sharing options...
Barand Posted September 23, 2014 Share Posted September 23, 2014 It seems perfectly reasonable to me that when you want to filter an array then use array_filter(). Link to comment https://forums.phpfreaks.com/topic/291231-copy-into-a-table-values-from-another-table-that-partially-match-a-given-value-case-insensitively/#findComment-1491860 Share on other sites More sharing options...
phdphd Posted September 23, 2014 Author Share Posted September 23, 2014 Both approaches are OK for me, since they yield more or less the same performances against a 30k-row table. Link to comment https://forums.phpfreaks.com/topic/291231-copy-into-a-table-values-from-another-table-that-partially-match-a-given-value-case-insensitively/#findComment-1491919 Share on other sites More sharing options...
Jacques1 Posted September 24, 2014 Share Posted September 24, 2014 You see no problem with code where you immediately ran into an unexpected problem, worked on it for more than half an hour and had to look up a feature which you didn't even know before? Well, how do I explain that ... If you ever work in a team or on a project that lives longer than a few months, one of the first things you learn is that fancy solutions are bad solutions. You do not want to keep everybody busy looking up exotic features in the PHP manual or fighting unexpected scope issues like you did. This is a waste of time and a sure way to get bugs. Code should be simple. So if you have the choice between a plain loop (which any PHP programmer can understand in a fraction of a second) and a super-fancy one-liner utilizing the latest PHP features (which people may or may not know), it should be obvious which one you go with. Now, if this is just a hobby project with you being the only programmer, you can of course do whatever you like and use the code primarily to show off your new skills. But for anything else, you want a loop. Link to comment https://forums.phpfreaks.com/topic/291231-copy-into-a-table-values-from-another-table-that-partially-match-a-given-value-case-insensitively/#findComment-1491931 Share on other sites More sharing options...
phdphd Posted September 25, 2014 Author Share Posted September 25, 2014 Sorry, Barand, I was so pleased to learn your "super-fancy one-liner" "showing off" new stuff to me . Link to comment https://forums.phpfreaks.com/topic/291231-copy-into-a-table-values-from-another-table-that-partially-match-a-given-value-case-insensitively/#findComment-1492045 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.