PHP Compare 2 Arrays find difference & case insensitive


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PHP Compare 2 Arrays find difference & case insensitive
# 1  
Old 12-25-2009
PHP Compare 2 Arrays find difference & case insensitive

Hi,

I need an elegant solotion in php. I need to compare 2 arrays (array1 & array2), to find all instances of array 2 which is not in array1.

I don't want to see any instances of array1 wich is not in array2 (here "the")

Array1: This, is, the, data, of, array1
Array2: this, is, data, Of, array2

result: array2

There are several solutions like array_diff, but I did not find a way to make this compare also case insensitive...
# 2  
Old 01-03-2010
Code:
print_r(array_diff(array_map('strtolower', $arr2),array_map('strtolower', $arr1)));

# 3  
Old 01-04-2010
a perl sample script
Code:
my @tmp1=qw(This is the data of array1);
my @tmp2=qw(this is the data of array2);
sub comp{
	my @t1=@{$_[0]};
	my @t2=@{$_[1]};
	my $flag=$_[2] || 'Y';
	foreach my $item (@t2){
		my $reg = ($flag eq 'N')?qr/$item/i:qr/$item/;
		my $cnt = () = grep {$_=~/$reg/} @t1;
		#print "-->",$item,"-->",$cnt,"\n";
		print $item,"\n" if $cnt == 0;
	}
}
comp(\@tmp1,\@tmp2,'N');

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find & Replace with same case letters

I have text with upper and lower case words. I want to find something and replace it with something new. But it should match the case - Meaning - it should replace old upper cased word with NEW upper case word and lower with lower. example: this text is very simple TEXT. now I want to replace... (5 Replies)
Discussion started by: grep_me
5 Replies

2. UNIX for Dummies Questions & Answers

Using FIND with case insensitive search

I am using HP-Unix B.11.31. Question: How to do the case insensitive search using FIND? Example: I would like list the files with extension of *.SQL & *.sql. When I try with command find . -type f -name *.sql, it does not lists file with *.SQL. (5 Replies)
Discussion started by: Siva SQL
5 Replies

3. UNIX for Dummies Questions & Answers

Using sed for case insensitive search

Hi, I have a file named "test_file" that has the below content. It has words in upper/lower cases PRODOPS prodOPS ProdOps PRODops escalate Shell My requirement is to replace all the "prodops" (what ever case it may be) with "productionoperations". I tried using the "i" option with... (7 Replies)
Discussion started by: sbhuvana20
7 Replies

4. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

5. Shell Programming and Scripting

Case Insensitive search

Hey , i am trying to do a search for the certain books , and im trying to make it case insensitive. what i have come up with so far is this : Database.txt RETARDED MONKEY:RACHEAL ABRAHAML:30:30:20 GOLD:FATIN:23.20:12:3 STUPID:JERLYN:20:40:3 echo -n "Title: " read Title echo -n... (3 Replies)
Discussion started by: gregarion
3 Replies

6. Shell Programming and Scripting

How to find difference between two arrays in Perl?

Hi, Could someone please help me with this? I have two arrays and I need to generate third array containing difference between the two. For example - @Array1 = ; @Array2 = ; I would like to find difference between these two and generate third array that contains the difference. In... (5 Replies)
Discussion started by: sncoupons
5 Replies

7. Shell Programming and Scripting

case-insensitive if on substring

I'd like to print a line if a substring is matched in a case insensitive manner something like do a case insensitive search for ABCD as a substring: awk '{ if (substr($1,1,4) == "") print $1 }' infile > outfile I'm not certain how to make the syntax work??? Thanks (4 Replies)
Discussion started by: dcfargo
4 Replies

8. Shell Programming and Scripting

case insensitive

hi everyone, I need to do the following thing in a case insesitive mode sed 's/work/job/g' filename since work could appear in different form as Work WORK WorK wORK,.... I was wondering if i could do a case insensitive search of a word. thanks in advance, :) (4 Replies)
Discussion started by: ROOZ
4 Replies

9. Shell Programming and Scripting

awk case-insensitive

can I tell awk to be case insensitive for one operation without setting the ignorecase value ? thanks, Steffen (7 Replies)
Discussion started by: forever_49ers
7 Replies

10. UNIX for Dummies Questions & Answers

case insensitive locate

How can I do a case insensitive locate? (3 Replies)
Discussion started by: davis.ml
3 Replies
Login or Register to Ask a Question