Solved

How to use 'array_unique" on transposed strings

Posted on 2011-10-15
216 Views

I have an array with the following values:

Blue ball
Red ball
Small green ball
Ball red
Ball blue
Big orange ball
Blue ball

array_unique will remove the duplicate "blue ball", but will not treat the transposed string  "ball blue" as a dupe.

How can I add this logic?

many thanks,

GJ
0
Question by:ggjones

LVL 14

Expert Comment

The only thing that comes to mind would be to take each array element and split the value into an array of words, sort the resulting arrays and then compare each array to the others.  Delete one if it is a duplicate of another.

Here are some code fragments to give you an idea of what I am thinking...

\$a[0] = 'Blue ball';
\$a[1] = 'Ball blue';

\$b[0] = asort(explode('',\$a[0]));  // after sorting - \$b[0][0] = ball  and \$b[0][1] = Blue;
\$b[1] = asort(explode('',\$a[1]));  // \$b[1][0] = Ball  and \$b[1][1] = ball;

if (\$b[0] == \$b[1]){
unset(\$a[1]);
unset(\$b1);
}

You would need to add a conversion to upper case to get matches, put this into a loop to loop through all array elements to generate your exploded arrays and also check syntax.

0

Author Comment

A bit too conceptual for me to take any further, I'm afraid :-)

regards,

GJ
0

LVL 34

Accepted Solution

Try this code

``````<?php

\$arr = array(
'Blue ball',
'Red ball',
'Small green ball',
'Ball red',
'Ball blue',
'Big orange ball',
'Blue ball'
);

function cleanArray( \$arr ) {

\$tempArr = array_unique( \$arr );
\$result = array();

// Process the array
//
foreach( \$tempArr as \$index => \$string ) {

// Convert the string to lower case and spilt it
//
\$exp = explode(" ", strtolower( \$string ) );

// Sort the resulting array and convert it back to a string
//
asort( \$exp );
\$normalisedString = implode( " ", \$exp );

// Check if the new string has already been seen before. If not record it and its index
//
if ( array_search( \$normalisedString, \$result ) === false )
\$result [\$index] = \$normalisedString;
}

// Array processed and all duplicates removed. Build the new results array
//
\$newArr = array();
foreach( \$result as \$index => \$string )
\$newArr [ \$index ] = \$arr [\$index];

return \$newArr;

}

echo "<pre>";
print_r(\$arr);
echo "</pre>";
echo "<br/>";

\$r = cleanArray( \$arr );

echo "<pre>";
print_r(\$r);
echo "</pre>";
echo "<br/>";
``````

which gives this output

Array
(
[0] => Blue ball
[1] => Red ball
[2] => Small green ball
[3] => Ball red
[4] => Ball blue
[5] => Big orange ball
[6] => Blue ball
)

Array
(
[0] => Blue ball
[1] => Red ball
[2] => Small green ball
[5] => Big orange ball
)

If you want continuous indices in the result array then change line 45 in the snippet above to be

\$newArr [] = \$arr [\$index];

0

Author Closing Comment

... this works fabulously bportlock; thank you very much.

I have a use case that is a variation on this, that you might have some insight on.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27398771.html

regards,

GJ

.
0

Featured Post

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!