2013-02-28 20 views
8

Dire che ho una stringa come questa $thestring = "1,2,3,8,2".Esplodere interi separati da virgole a intvals?

Se I explode(',', $thestring) esso, ottengo una matrice di stringhe. Come faccio a esplodere su una matrice di numeri interi?

+0

la stringa contiene numeri interi e solo virgola ?? –

+0

@User, solo numeri interi e virgole, nient'altro –

+1

Controlla questo post: http://stackoverflow.com/questions/9593765/how-to-convert-array-values-from-string-to-int –

risposta

9

usare qualcosa di simile a questo:

$data = explode(',', $thestring); 
array_walk($data, 'intval'); 

http://php.net/manual/en/function.array-walk.php

+0

Sì, ho notato che avevo torto e che stavo per modificarlo. Ma trovo che i documenti PHP siano confusi, dice callable per primo argomento. –

+2

@AmigableClarkKant, vedere il collegamento, callable è il secondo, il primo è array. Ma comunque, sì, questo è uno dei maggiori problemi di PHP. Alcune funzioni sono con trattino basso, altre no. Alcuni hanno un ordine, altri hanno il rovescio. Strano. –

+0

@ Prof.Falken, penso che pensi a array_map? – MJoraid

0

Da $ TheString è una stringa allora si ottiene un array di stringhe.

Basta aggiungere (int) di fronte ai valori esplosi.

Oppure utilizzare la funzione array_walk:

$arr = explode(',', $thestring); 
array_walk($arr, 'intval'); 
+0

non funziona per me checkout questo link http://codepad.org/WwpN4J2x – Kathir

0

Per la maggior parte non si dovrebbe davvero bisogno di (PHP è generalmente buono con gestione delle stringhe di fusione e galleggia/int), ma se è assolutamente necessario, si può array_walk con intval o floatval:

$arr = explode(',','1,2,3'); 
// use floatval if you think you are going to have decimals 
array_walk($arr,'intval'); 
print_r($arr); 

Array 
(
    [0] => 1 
    [1] => 2 
    [2] => 3 
) 

Se avete bisogno di qualcosa di un po 'più dettagliato, si può anche prendere in considerazione settype:

$arr = explode(",","1,2,3"); 
function fn(&$a){settype($a,"int");} 
array_walk($f,"fn"); 
print_r($f); 

Array 
(
    [0] => 1 
    [1] => 2 
    [2] => 3 
) 

Questo potrebbe essere particolarmente utile se si sta cercando di lanciare in modo dinamico:

class Converter { 
    public $type = 'int'; 
    public function cast(&$val){ settype($val, $this->type); } 
} 
$c = new Converter(); 

$arr = explode(",","1,2,3,0"); 
array_walk($arr,array($c, 'cast')); 
print_r($arr); 

Array 
(
    [0] => 1 
    [1] => 2 
    [2] => 3 
    [3] => 0 
) 

// now using a bool 
$c->type = 'bool'; 
$arr = explode(",","1,2,3,0"); 
array_walk($arr,array($c, 'cast')); 
var_dump($arr); // using var_dump because the output is clearer. 

array(4) { 
    [0]=> 
    bool(true) 
    [1]=> 
    bool(true) 
    [2]=> 
    bool(true) 
    [3]=> 
    bool(false) 
} 
1
$thestring = "1,2,3,8,a,b,2"; 

$newArray = array(); 
$theArray = explode(",", $thestring); 

print_r($theArray); 

foreach ($theArray as $theData) { 
    if (is_numeric($theData)) { 
    $newArray[] = $theData; 
    } 
} 

print_r($newArray); 

// uscita

matrice originale

Array ([0] => 1 [1] => 2 [2] => 3 [3] => 8 [4] => a [5] => b [6] => 2) 

numerico solo serie

Array ([0] => 1 [1] => 2 [2] => 3 [3] => 8 [4] => 2) 
17

array_map anche potrebbe essere usata:

$s = "1,2,3,8,2"; 
$ints = array_map('intval', explode(',', $s)); 
var_dump($ints); 

uscita:

array(5) { 
    [0]=>  int(1) 
    [1]=>  int(2) 
    [2]=>  int(3) 
    [3]=>  int(8) 
    [4]=>  int(2) 
} 

Esempio codepad.

1
$arr=explode(',', $thestring); 
$newstr = ''; 
foreach($arr as $key=>$val){ 
    $newstr .= $val; 
} 
+2

Non penso che procuda il risultato atteso, sarà produrre una stringa come '12382'. – biziclop