2016-06-01 70 views
0

我在javascript中有一個數組,用,分隔。但在他們的內部有一些價值包含,,因爲它的價值。我的問題是我不想爆炸這些值,但PHP做到了。有什麼辦法來處理它? array[0]='sports, music,videos'如何不在PHP中的數組中間爆炸逗號

<form id="form" method="post"> 
<table> 
<tr val="sprots, music, videos"><td>sprots, music, videos</td></tr> 
...... 
...... 
<tr val="car"><td>car</td></tr> 
</table> 
<input type="hidden" name="category" id="category" value=""> 
</form> 
<script> 
    var Data=[]; 
    $('table').find('tr').each(function(){ 
     Data.push($(this).attr('val')); 
    }); 
$('#category').val(Data); 
</script> 

<?php 
$category=$_POST['category']; 
$tmp=explode(',',$category); 
?> 

的問題,當它要發生爆炸這樣的值發生。它爆炸爲3分離的數組,這是這樣的: array [0] ='sports',array [1] ='music',array [2] ='videos'。 我想爆炸這個值作爲一個獨特的一部分,我的意思是這樣的: array[0]='sports, music,videos'

+0

秀'的var_dump($類); '從你的代碼 – RomanPerekhrest

+0

你可能需要內爆而不是爆炸.. –

+0

然後不要'爆炸'。或者首先從字符串中刪除你不會被爆炸的東西。 –

回答

1

爲迪內希建議你可以改變的,而不是逗號,需要的東西,永遠不會給你帶來麻煩

爲例分離,:

var Data=[]; 
$('table').find('tr').each(function(){ 
    Data.push('~'+ $(this).attr('val')); 
}); 
console.log(Data); 

輸出:

["~sprots, music, videos", "~car"] 

但是當你發佈的數據就會變得像~sprots, music, videos,~car 在服務器端,你必須做這樣的事情

<?php 
$category=$_POST['category']; 
$tmp=explode('~',$category); 
unset($tmp[0]); 
var_dump($tmp); 
?> 

輸出:

array(2) { 
    [1] => 
    string(22) "sprots, music, videos," 
    [2] => 
    string(3) "car" 
} 
+0

我試過這個,但是它不會在第一個值處加上'〜'。它出什麼問題了? –

+0

如果輸出結果如你所說,沒有問題,但輸出是這樣的:'[〜體育,音樂,視頻,〜汽車]'。它增加了額外的',',因爲視頻之後的'''將被放入數組[0]中。 –

+0

我不清楚。你可以添加你的輸入和輸出 – chudasamachirag