2014-02-08 136 views
0

我有串,看起來像這樣:如何從字符串創建數組?

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'; 
} 

而且我執行了上述更多的陳述。

+1

'「>'是出於幾個原因做錯的方法。改用會話。 –

+2

'print_r'是不可逆的。使用像'implode/explode',''json_encode()/ json_decode()'或'serialize()/ unserialize()',(並且注意XSS攻擊......用'htmlspecialchars()'來避開)。但是約翰·孔德的評論認爲它不應該放在首位,甚至是更正確的。 – Wrikken

+0

@JohnConde我不明白你的意思。如果您知道答案,請回答問題 – nitrous

回答

1

正如Wrikken在評論print_r()中提到的是不可逆的。因此,您使用print_r()進行回顯後,該數組中的數據無法重新填充到變量中。如果您的目標是將這些值放入您的表單以供稍後提交和處理,則需要選擇可逆的的格式。有幾個方法可以做到這一點:

使用json_encode()/json_decode()

這將改變你的陣列成JSON格式(和回來):

$array = array('movies', 'games', 'art', 'books', 'cars', 'business', 'lifestyle'); 
echo json_encode($array); 
// output: ["movies","games","art","books","cars","business","lifestyle"] 

// Reading back in 
$array = json_decode($_POST['interests'], true); 

使用serialize()/unserialize()

$array = array('movies', 'games', 'art', 'books', 'cars', 'business', 'lifestyle'); 
echo serialize($array); 
// output: a:7:{i:0;s:6:"movies";i:1;s:5:"games";i:2;s:3:"art";i:3;s:5:"books";i:4;s:4:"cars";i:5;s:8:"business";i:6;s:9:"lifestyle";} 

// Reading back in 
$array = unserialize($_POST['interests']); 

使用逗號分隔字符串implode()/explode()

對於像你這樣非常基本的數據,只是把值放入用逗號分隔,也可以有效的字符串:

$array = array('movies', 'games', 'art', 'books', 'cars', 'business', 'lifestyle'); 
echo serialize($array); 
// output: movies,games,art,books,cars,business,lifestyle 

// Reading back in 
$array = explode($_POST['interests']); 

然而,更好解決方案將使用sessions。會話允許您從頁面到頁面傳輸數據,而不必將數據發送到客戶端,只將數據發送回服務器。

在第1頁:

<?php 
session_start(); 
$interests = array('movies', 'games', 'art', 'books', 'cars', 'business', 'lifestyle'); 
$_SESSION['interests'] = $array; 

在第2頁:

<?php 
session_start(); 
$interests = $_SESSION['interests']; 
print_r($interests); 
// Output: Array ([0] => movies [1] => games [2] => art [3] => books [4] => cars [5] => business [6] => lifestyle)