Uhm... qualche tempo fa avevo un problema simile, vediamo se ricordo bene la soluzione che ho implementato.
*******************
Soluzione 1:
Puoi utilizzare un array associativo (cioè una tabella hash, anzichè 2 array separati) fatto in questo modo:
$mioarray["scarpe"] = "2133";
$mioarray["pantaloni"] = "5222";
$mioarray["giacche"] = "7844";
$mioarray["calzettoni"] = "9436";
...
e così via.
e poi ordini alfabeticamente tramite la chiave (che è la tipologia del prodotto) -->> vedi guida php riguardo alle funzioni che ordinano gli array associativi (oppure la funzione che ho copia-incollato qui sotto).
*******************
Soluzione 2 (meno efficiente e meno elegante della precedente):
Su http://it.php.net/manual/it/function.strcmp.php
ho trovato il seguente codice di funzione che ordina alfabeticamente due stringhe:
codice:
// Returns 1 if $str1 comes before $str2 alphabetically
// Returns -1 if $str1 comes after $str2 alphabetically
// Returns 0 if $str1 and $str2 are the same
function orderAlpha ( $str1, $str2 ) {
$limit = null;
if ( strlen( $str1 ) > strlen( $str2 ) ) {
$limit = strlen( $str2 );
} else {
$limit = strlen( $str1 );
}
for ( $i = 0;$i < $limit;$i++ ) {
if ( substr( $str1, $i, 1 ) > substr( $str2, $i, 1 ) ) {
return 1;
} else if ( substr( $str1, $i, 1 ) < substr( $str2, $i, 1 ) ) {
return -1;
}
}
if ( strlen( $str1 ) > strlen( $str2 ) ) {
return 1;
} else if ( strlen( $str1 ) < strlen( $str2 ) ) {
return -1;
}
return 0;
}
// Case insensitive version of orderAlpha
function orderiAlpha ( $str1, $str2 ) {
return orderAlpha( strtolower( $str1 ), strtolower( $str2 ) );
}
Supponiamo adesso che i tuoi due array si chiamino $tip_array e $idp_array .
Per ordinarli puoi procedere così:
codice:
for ($i=0; $i<26; $i++)
for ($j=$i+1; $j<26; $j++)
if (orderAlpha ( $tip_array[$i], $tip_array[$j])<0) {
// scambiamo le tipologie
$swap_tip=$tip_array[$i];
$tip_array[$i]=$tip_array[$j];
$tip_array[$j]=$swap_tip;
// scambiamo anche gli id prodotto
$swap_idp=$idp_array[$i];
$idp_array[$i]=$idp_array[$j];
$idp_array[$j]=$swap_idp;
} // chiude l'if