PDA

View Full Version : sort strings using data from another array


mudoeb
04-21-2008, 10:40 AM
i have two arrays with strings and numbers with equal number of characters
string3 ..... 3
string2 ..... 1
string5 ..... 1
string2 ..... 3
string1 ..... 5

- how to sort first list of strings in order of array with numbers ?
string2 ..... 1
string5 ..... 1
string2 ..... 3
string3 ..... 3
string1 ..... 5

- how to remove all strings below exact point (number) in second array ? for example below number 3
string2 ..... 1
string5 ..... 1
string2 ..... 3
string3 ..... 3

Mwnciau
04-21-2008, 03:28 PM
For your first question, you can use array_multisort (http://uk.php.net/array_multisort):

$strings = array ( "string1", "string2", "string3", "string4", "string5" );
$numbers = array ( 2, 5, 3, 1, 1 );

array_multisort ( $numbers, SORT_ASC, SORT_NUMERIC, $strings );

For your second you can use a for or foreach loop:


$parameter = 3;

foreach ( $strings as $key => $value )
{
if ( $numbers[$key] > $parameter )
{
unset ( $strings[$key] );
unset ( $numbers[$key] );
}
}