• Status: Solved
• Priority: Medium
• Security: Public
• Views: 272

# How to use 'array_unique" on transposed strings

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
ggjones
• 2
1 Solution

Commented:
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 Commented:

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

regards,

GJ
0

Commented:
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 Commented:
... 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

• 2
Tackle projects and never again get stuck behind a technical roadblock.