我有串,看起來像這樣:如何從字符串創建數組?
Array ([0] => movies [1] => games [2] => art [3] => books [4] => cars [5] => business [6] => lifestyle)
我想打一個數組出這個字符串。我動態生成這個字符串,我不知道這些值是什麼。
Array ([0] => Array (movies [0] => games [1] => art [2] => books [3] => cars [4] => business [5] => lifestyle))
我試圖提取嵌套數組像這樣:$interests = $_POST['interests'][0];
(數組利益是從複選框形式)但是,當我做到這一點,它把我從一個數組,看起來像這樣得到這個字符串嵌套數組作爲字符串而不是數組。我知道這是因爲我用旁邊的in_array
功能,它拋出我這個錯誤:
Warning: in_array() expects parameter 2 to be array, string given in /Application/...
我不知道爲什麼它處理嵌套數組作爲字符串,但它是。那麼如何將字符串轉換回數組。
* UPDATE:*
我的HTML看起來像這樣:
<form action = 'signup.php?step=2' method='post'>
<h3>Interests</h3>
<div class="interest_chooser">
<label><input type="checkbox" value="music" name="interest[]"> Music<br></label>
<label><input type="checkbox" value="movies" name="interest[]"> Movies<br></label>
<label><input type="checkbox" value="games" name="interest[]"> Games<br></label>
<label><input type="checkbox" value="art" name="interest[]"> Art<br></label>
<label><input type="checkbox" value="books" name="interest[]"> Books<br></label>
<label><input type="checkbox" value="cars" name="interest[]"> Cars<br></label>
<label><input type="checkbox" value="ideas" name="interest[]"> Ideas<br></label>
<label><input type="checkbox" value="business" name="interest[]"> Business<br></label>
<label><input type="checkbox" value="comedy" name="interest[]"> Comedy<br></label>
<label><input type="checkbox" value="technology" name="interest[]"> Technology<br></label>
<label><input type="checkbox" value="lifestyle" name="interest[]"> Lifestyle<br></label>
</div>
<input type = 'submit'>
</form>
而且我signup.php步= 2是這樣的:
<form action = 'signup.php?step=3' method='post'>
<input type="hidden" name="interests" value="<?php print_r($_POST['interest']); ?>">
<!-- More Inputs that I will not show because they are irrelevant -->
<input type='submit'>
</form>
而我註冊.php?step = 3提交數據和我得到陣列的部分如下所示:
$interests = $_POST['interests'][0];
if (in_array('music', $interests)) {
$music = 'music';
} else {
$music = 'no_val';
}
而且我執行了上述更多的陳述。
'「>'是出於幾個原因做錯的方法。改用會話。 –
'print_r'是不可逆的。使用像'implode/explode',''json_encode()/ json_decode()'或'serialize()/ unserialize()',(並且注意XSS攻擊......用'htmlspecialchars()'來避開)。但是約翰·孔德的評論認爲它不應該放在首位,甚至是更正確的。 – Wrikken
@JohnConde我不明白你的意思。如果您知道答案,請回答問題 – nitrous