Avendo delle stringhe tipo:
1-4;
1-2;
2-4;
ecc...
Come posso ottenere degli array tipo
chiave 1=> valore 4;
chiave 1=> valore 2;
chiave 2=> valore 4;
Grazie
Avendo delle stringhe tipo:
1-4;
1-2;
2-4;
ecc...
Come posso ottenere degli array tipo
chiave 1=> valore 4;
chiave 1=> valore 2;
chiave 2=> valore 4;
Grazie
per ogni stringa esegui
Codice PHP:
$vet1=explode(";",$stringa);
$vet2=explode("-",$vet1[0]);
$vettore[$vet2[0]]=$vet2[1];
riguardando ho visto che la chiave non è univoca quindi non va bene
Codice PHP:
$stringhe = array("1-4;",
"1-2;",
"2-4;");
$arr = array();
foreach($stringhe as $stringa){
$vet1=explode(";",$stringa);
$vet2=explode("-",$vet1[0]);
if(!array_key_exists($vet2[0],$arr)){
$arr[$vet2[0]] = $vet2[1];
}else{
if(!is_array($arr[$vet2[0]])){
$arr[$vet2[0]] = array($arr[$vet2[0]],$vet2[1]);
}else{
$arr[$vet2[0]][] = $vet2[1];
}
}
}
IP-PBX management: http://www.easypbx.it
Old account: 2126 messages
Oldest account: 3559 messages
Grazie va bene la prima soluzione perchè le chiavi sono univoche. Avevo solo sbagliato l'esempio.
Grazie