Jump to content

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


phdphd
Go to solution Solved by 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.

Link to comment
Share on other sites

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
Share on other sites

  • Solution

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"
}
Edited by phdphd
Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.