2015-10-19 20 views
0

我在這裏有一個問題,在這裏如何解析$POST_['value'] (e.g 3785,3789,3790,3787)從一個表格到array($POST_['value'])並做foreach。請參閱下面的示例代碼:

function someFunction(){ 
    $html = ''; 
    $int = $_POST['Ids']; //POST the value as 3785,3789,3790,3787 
    $IDs = array($int); 

    foreach ($IDs as $ID) { 
     $intVal = '<int>' . $ID .'</int>'; 
     $html .= $intVal; 
    } 

    return $html; 
} 

但是結果顯示它爲****整個字符串**而不是數組**。 如果我把array(3785,3789,3790,3787)這樣,它會解析爲數組在foreach中。如何將$POST_['IDs']轉換爲數字或某種以便被識別爲數組?

感謝

邁克

+0

[如何轉換陣列的可能的複製從字符串到int的值?](http://stackoverflow.com/questions/9593765/how-to-convert-array-values-from-string-to-int) –

回答

1

這將工作

function someFunction(){ 
    $html = ''; 
    $int = $_POST['Ids']; //POST the value as 3785,3789,3790,3787 
    $IDs = explode(',', $_POST['Ids']); 

    foreach ($IDs as $ID) { 
     $intVal = '<int>' . $ID .'</int>'; 
     $html .= $intVal; 
    } 

    return $html; 
} 
+0

這很好,謝謝,也許我不明白爆炸足夠了,我知道它的基本。 –

+0

沒問題@MikeeTsai –

0

你需要做一個快速解決下面一行:

<?php 

$IDs = explode(",", $int);