Jump to content

Copy Into a table values from another table that partially match a given value, case-insensitively


phdphd

Recommended Posts

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.

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;});

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) {
}

I got it :happy-04:

$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"
}

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.

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.

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.