我有串,看起来像这样:如何从字符串创建数组?
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